A. 如何修改安卓手機的通知欄時間、日期和通知顏色
安卓手機通知欄個性化教程對於追求個性化界面的android手機用戶,修改通知欄時間、日期和通知顏色可以為手機增添新鮮感。以下是詳細的步驟:
一、修改時間顏色
二、修改日期顏色
三、修改通知字體顏色
B. android8.0之懸浮窗和通知欄
懸浮窗:
使用場景:例如微信在視頻的時候,點擊Home鍵,視頻小窗口仍然會在屏幕上顯示;
注意事項:
1、一般需要在後台進行操作的時候才需要懸浮窗,這樣懸浮窗才有意義;
2、API Level >= 23的時候,需要在AndroidManefest.xml文件中聲明許可權SYSTEM_ALERT_WINDOW才能在其他應用上繪制控制項。
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />;除了這個許可權外,我們還需要在系統設置裡面對本應用進行設置懸浮窗許可權。該許可權在應用中需要啟動Settings.ACTION_MANAGE_OVERLAY_PERMISSION來讓用戶手動設置許可權:startActivityForResult(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName())), REQUEST_CODE);
3、LayoutParam設置:LayoutParam里的type變數。這個變數是用來指定窗口類型的。在設置這個變數時,需要注意一個坑,那就是需要對不同版本的Android系統進行適配。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
};在Android 8.0之前,懸浮窗口設置可以為TYPE_PHONE,這種類型是用於提供用戶交互操作的非應用窗口。
而Android 8.0對系統和API行為做了修改,包括使用SYSTEM_ALERT_WINDOW許可權的應用無法再使用一下窗口類型來在其他應用和窗口上方顯示提醒窗口:
- TYPE_PHONE
- TYPE_PRIORITY_PHONE
- TYPE_SYSTEM_ALERT
- TYPE_SYSTEM_OVERLAY
- TYPE_SYSTEM_ERROR
如果需要實現在其他應用和窗口上方顯示提醒窗口,那麼必須該為TYPE_APPLICATION_OVERLAY的新類型;
如果在Android 8.0以上版本仍然使用TYPE_PHONE類型的懸浮窗口,則會出現如下異常信息:
android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@f8ec928 -- permission denied for window type 2002;
具體實現:
1、Activity:
public void startFloatingService(View view) {
...
if (!Settings.canDrawOverlays(this)) {
Toast.makeText(this, "當前無許可權,請授權", Toast.LENGTH_SHORT);
startActivityForResult(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName())), 0);
} else {
startService(new Intent(MainActivity.this, FloatingService.class));
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0) {
if (!Settings.canDrawOverlays(this)) {
Toast.makeText(this, "授權失敗", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "授權成功", Toast.LENGTH_SHORT).show();
startService(new Intent(MainActivity.this, FloatingService.class));
}
}
}
2、service:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
showFloatingWindow();
return super.onStartCommand(intent, flags, startId);
}
private void showFloatingWindow() {
if (Settings.canDrawOverlays(this)) {
// 獲取WindowManager服務
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
// 新建懸浮窗控制項
Button button = new Button(getApplicationContext());
button.setText("Floating Window");
button.setBackgroundColor(Color.BLUE);
// 設置LayoutParam
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
}
layoutParams.format = PixelFormat.RGBA_8888;
layoutParams.width = 500;
layoutParams.height = 100;
layoutParams.x = 300;
layoutParams.y = 300;
// 將懸浮窗控制項添加到WindowManager
windowManager.addView(button, layoutParams);
}
}
效果展示:
C. android 4.2 怎樣禁止通知欄下拉呢
1.在AndroidManifest.xml中添加許可權
<uses-permission android:name="android.permission.EXPAND_STATUS_BAR"/>
<uses-permission android:name="android.permission.STATUS_BAR"/>
2.在相應的activity中添加
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
try {
Object service = getSystemService("statusbar");
Class<?> statusbarManager = Class.forName("android.app.StatusBarManager");
Method test = statusbarManager.getMethod("collapse");
test.invoke(service);
} catch (Exception ex) {
ex.printStackTrace();
}
}
D. android通知欄怎麼添加控制項
Notification的自定義布局是RemoteViews,和其他RemoteViews一樣,在自定義視圖布局文件中,僅支持FrameLayout、LinearLayout、RelativeLayout三種布局控制項和AnalogClock、Chronometer、Button、ImageButton、ImageView、ProgressBar、TextView、ViewFlipper、ListView、GridView、StackView和AdapterViewFlipper這些顯示控制項,不支持這些類的子類或Android提供的其他控制項。否則會引起ClassNotFoundException異常
步驟如下:
1)創建自定義視圖
2)獲取遠程視圖對象(註:Notification的contentView不能為空)
3)設置PendingIntent(來響應各種事件)
4)發起Notification
大體4步驟這里就不詳細說了,下面就把DEMO中的列子拿出來說下
樣式:
1.自定義帶按鈕通知欄(如下樣式)
正在進行的
「正在進行的」通知使用戶了解正在運行的後台進程。例如,音樂播放器可以顯示正在播放的音樂。也可以用來顯示需要長時間處理的操作,例如下載或編碼視頻。「正在進行的」通知不能被手動刪除。
實現方法如下:
java">實現方法如下:
/**
*帶按鈕的通知欄
*/
publicvoidshowButtonNotify(){
NotificationCompat.BuildermBuilder=newBuilder(this);
RemoteViewsmRemoteViews=newRemoteViews(getPackageName(),R.layout.view_custom_button);
mRemoteViews.setImageViewResource(R.id.custom_song_icon,R.drawable.sing_icon);
//API3.0以上的時候顯示按鈕,否則消失
mRemoteViews.setTextViewText(R.id.tv_custom_song_singer,"周傑倫");
mRemoteViews.setTextViewText(R.id.tv_custom_song_name,"七里香");
//如果版本號低於(3。0),那麼不顯示按鈕
if(BaseTools.getSystemVersion()<=9){
mRemoteViews.setViewVisibility(R.id.ll_custom_button,View.GONE);
}else{
mRemoteViews.setViewVisibility(R.id.ll_custom_button,View.VISIBLE);
}
//
if(isPlay){
mRemoteViews.setImageViewResource(R.id.btn_custom_play,R.drawable.btn_pause);
}else{
mRemoteViews.setImageViewResource(R.id.btn_custom_play,R.drawable.btn_play);
}
//點擊的事件處理
IntentbuttonIntent=newIntent(ACTION_BUTTON);
/*上一首按鈕*/
buttonIntent.putExtra(INTENT_BUTTONID_TAG,BUTTON_PREV_ID);
//這里加了廣播,所及INTENT的必須用getBroadcast方法
PendingIntentintent_prev=PendingIntent.getBroadcast(this,1,buttonIntent,PendingIntent.FLAG_UPDATE_CURRENT);
mRemoteViews.setOnClickPendingIntent(R.id.btn_custom_prev,intent_prev);
/*播放/暫停按鈕*/
buttonIntent.putExtra(INTENT_BUTTONID_TAG,BUTTON_PALY_ID);
PendingIntentintent_paly=PendingIntent.getBroadcast(this,2,buttonIntent,PendingIntent.FLAG_UPDATE_CURRENT);
mRemoteViews.setOnClickPendingIntent(R.id.btn_custom_play,intent_paly);
/*下一首按鈕*/
buttonIntent.putExtra(INTENT_BUTTONID_TAG,BUTTON_NEXT_ID);
PendingIntentintent_next=PendingIntent.getBroadcast(this,3,buttonIntent,PendingIntent.FLAG_UPDATE_CURRENT);
mRemoteViews.setOnClickPendingIntent(R.id.btn_custom_next,intent_next);
mBuilder.setContent(mRemoteViews)
.setContentIntent(getDefalutIntent(Notification.FLAG_ONGOING_EVENT))
.setWhen(System.currentTimeMillis())//通知產生的時間,會在通知信息里顯示
.setTicker("正在播放")
.setPriority(Notification.PRIORITY_DEFAULT)//設置該通知優先順序
.setOngoing(true)
.setSmallIcon(R.drawable.sing_icon);
Notificationnotify=mBuilder.build();
notify.flags=Notification.FLAG_ONGOING_EVENT;
mNotificationManager.notify(notifyId,notify);
}
如果您對回答滿意,請關注一下俺的微博