溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何自定義狀態(tài)欄notification布局

發(fā)布時間:2021-12-13 17:37:50 來源:億速云 閱讀:273 作者:小新 欄目:移動開發(fā)

這篇文章主要介紹如何自定義狀態(tài)欄notification布局,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

布局定義custom_notification.xml

<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent" >  
      
    <ImageView   
        android:id="@+id/p_w_picpath"  
        android:layout_width="wrap_content"  
        android:layout_height="fill_parent"  
        android:layout_alignParentLeft="true"  
        android:layout_marginRight="10dp"  
        android:contentDescription="@string/Image" />  
      
    <TextView   
        android:id="@+id/title"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_toRightOf="@id/p_w_picpath"  
         />  
      
    <TextView   
        android:id="@+id/text"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_toRightOf="@id/p_w_picpath"  
        android:layout_below="@id/title"  
         />  
      
</RelativeLayout>

布居中引用的樣式文件styles.xml

<?xml version="1.0" encoding="utf-8"?>  
<resources>  
<style name="NotificationText" parent="android:TextAppearance.StatusBar.EventContent" />  
<style name="NotificationTitle" parent="android:TextAppearance.StatusBar.EventContent.Title" />  
</resources>

代碼

package cn.itcast.tabhost;



import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Color;
import android.widget.RemoteViews;

public  class FirstActivity extends Activity  {
    
    //默認點擊返回鍵(back)會finish當前activity
    //activity棧中的所有activity都彈出后會退出當前應用
    @Override
    public void onBackPressed() {
        
        /*
         * 按照一般的邏輯,當Activity棧中有且只有一個Activity時,當按下Back鍵此
         * 那么下次點擊此應用程序圖標將從重新啟動,當前不少應用程序都是采取如Home鍵的效果,
         * 當點擊了Back鍵,系統返回到桌面,然后點擊應用程序圖標
         * 直接回到之前的Activity界面,這種效果是怎么實現的呢?通過重寫按下Back鍵的回調函數,轉成Home鍵的效果即可。
         */
        // 改為使用intent啟動HOME桌面
        Intent home = new Intent(Intent.ACTION_MAIN);
        home.addCategory(Intent.CATEGORY_HOME);
        startActivity(home);

        // 或者,為達到此類效果,Activity實際上提供了直接的方法。
        // 將當前Activity所在的Task移到后臺,同時保留activity順序和狀態(tài)。
        moveTaskToBack(true);// true表示不管是不是根都有效
    }
    
    
        /**
         * 當此Activity處于后臺工作時, 在狀態(tài)欄顯示通知
         */
        @Override
        protected void onStop() {
            showNotification();
            super.onStop();
        }
    
    //當程序再次進入運行界面時,Activity處于onResume狀態(tài),在onResume方法中去掉狀態(tài)欄的程序運行信息即可
    
        /**
         * 此Activity啟動后關閉狀態(tài)欄的通知
         */
        @Override
        protected void onResume() {
            // 啟動后刪除之前我們定義的通知
            NotificationManager notificationManager = (NotificationManager) this
                    .getSystemService(NOTIFICATION_SERVICE);
            notificationManager.cancel(CUSTOM_VIEW_ID);
            super.onResume();
        }
        
        private static final int CUSTOM_VIEW_ID = 1; 
      //在狀態(tài)欄顯示程序通知
        private void showNotification() {
            // 創(chuàng)建一個NotificationManager的引用
            NotificationManager notificationManager = (NotificationManager) this
                    .getSystemService(android.content.Context.NOTIFICATION_SERVICE);
    
            // 定義Notification的各種屬性
            Notification notification = new Notification(R.drawable.bg_normal,
                    "superGao", System.currentTimeMillis());
            
            
            
            RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification); 
            contentView.setImageViewResource(R.id.p_w_picpath, R.drawable.i1);  
            contentView.setTextViewText(R.id.title, "自定義布局通知標題");  
            contentView.setTextViewText(R.id.text, "自定義布局通知內容"); 
            //給view設置點擊事件
       /*     contentView.setOnClickPendingIntent(viewId, pendingIntent);
            */
            
            notification.contentView = contentView; 
            
            notification.flags |= Notification.FLAG_ONGOING_EVENT; // 將此通知放到通知欄的"Ongoing"即"正在運行"組中
            notification.flags |= Notification.FLAG_NO_CLEAR; // 表明在點擊了通知欄中的"清除通知"后,此通知不清除,經常與FLAG_ONGOING_EVENT一起使用
            notification.flags |= Notification.FLAG_SHOW_LIGHTS;//使用LED燈
            notification.defaults = Notification.DEFAULT_LIGHTS;
            notification.ledARGB = Color.BLUE;//LED燈顏色
            notification.ledOnMS = 5000;//led燈持續(xù)時間
    
            // 設置通知的事件消息
            /*
             * CharSequence contentTitle = "superGao"; // 通知欄標題
                CharSequence contentText = "love"; // 通知欄內容
            */
            Intent notificationIntent = new Intent(this, FirstActivity.class); // 點擊該通知后要跳轉的Activity
            PendingIntent contentItent = PendingIntent.getActivity(this, 0,
                    notificationIntent, 0);
            notification.contentIntent=contentItent;
            
           /* notification.setLatestEventInfo(this, contentTitle, contentText,
                    contentItent);*/
    
            // 把Notification傳遞給NotificationManager
            notificationManager.notify(CUSTOM_VIEW_ID , notification);
    
        }
    
}

以上是“如何自定義狀態(tài)欄notification布局”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI