導航:首頁 > 操作系統 > android通知欄代碼

android通知欄代碼

發布時間:2025-06-26 14:41:20

A. 如何修改安卓手機的通知欄時間、日期和通知顏色

安卓手機通知欄個性化教程

對於追求個性化界面的android手機用戶,修改通知欄時間、日期和通知顏色可以為手機增添新鮮感。以下是詳細的步驟:


一、修改時間顏色


1. 首先,確保電腦安裝了JDK,然後下載並解壓smali.rar工具包。從severce.jar中提取classes.dex文件,並使用baksmali.jar進行反編譯
2. 打開classes/com/android/server/status/statusbaricon.smali,找到const V6, -0X100,將其修改為const v6, -0x1,以將時間顏色從黑色改為白色。
3. 修改完成後,重新打包classes.dex,將其替換回severce.jar,並確保services.jar文件在/system下的許可權設置為rw-r--r--。
4. 重啟手機,即可看到時間顏色的變化。

二、修改日期顏色


1. 前四步與時間顏色修改相同,隨後在StatusBarService.smali中找到.line276,添加代碼以修改日期顏色。
2. 重啟手機,日期顏色也會隨之改變。

三、修改通知字體顏色


1. 從framework-res.apk中的三個文件status_bar_latest_event_content.xml、status_bar_expanded.xml和status_bar.xml控制通知字體顏色。
2. 使用16進制編輯器修改顏色代碼,如將黑色(00 00 00 FF)改為白色(FF FF FF FF)。
3. 重新打包framework-res.apk,更新到/system/framework/,確保許可權設置。

這些更改需要一定的技術操作,但完成後,您的安卓手機通知欄將煥然一新。如需進一步探索,可以參考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);
}

如果您對回答滿意,請關注一下俺的微博

閱讀全文

與android通知欄代碼相關的資料

熱點內容
程序員出席活動 瀏覽:106
程序員送給我的禮物 瀏覽:776
php按拼音排序 瀏覽:650
紅警1重製版資源源碼 瀏覽:459
騰訊雲代理伺服器代金券 瀏覽:994
2015版中國葯典pdf 瀏覽:124
pdf一張列印多頁 瀏覽:763
解壓神器233 瀏覽:392
按鍵手機版命令大全 瀏覽:606
php本周第一天 瀏覽:321
解壓玩具可以怎麼封口 瀏覽:518
java識別驗證碼ocr 瀏覽:39
個性化圖標怎麼設置安卓 瀏覽:787
塗磊程序員 瀏覽:188
手機模擬終端命令 瀏覽:616
紅底白色的心是什麼app的標志 瀏覽:66
安卓充電寶什麼牌子質量好又安全 瀏覽:452
linuxgettimeofday 瀏覽:399
鴻蒙手機平板如何交互安卓手機 瀏覽:989
京東app什麼時候有優惠 瀏覽:275