天天看點

Android之Notification使用

一種可以顯示即時資訊的控件,該控件顯示在标題欄中,拉開後會看到通知的完整樣式

notification有“普通通知”、“大視圖通知”、“進度條通知”、“自定義通知”四種樣式

首先是第一種普通通知,我在代碼裡面給大家詳細介紹一下吧

public class NotiFiActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_noti_fi);
        initView();
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void initView() {

        //得到NotificationCompat.Builder對象
        NotificationCompat.Builder builder=new NotificationCompat.Builder(getApplicationContext());

        builder.setContentTitle("我是标題");  //設定标題

        builder.setSmallIcon(R.drawable.ic_launcher);  //設定圖示

        builder.setContentText("我是通知内容");  //設定内容
        //以上3項是必須設定的


        //builder.setSound(Uri sound):設定一個鈴聲,用于在通知的時候響應。傳遞一個Uri的參數,格式為“file:///mnt/sdcard/Xxx.mp3”。
        builder.setLights(100, 10, 1);      //設定前置LED燈的閃爍速率,持續毫秒數,停頓毫秒數。
        builder.setVibrate(new long[]{10,20,20});    //設定震動的模式,以一個long數組儲存毫秒級間隔的震動。

        /** 大多數時候,我們并不需要設定一個特定的響應效果,隻需要遵照使用者裝置上系統通知的效果即可,
        那麼可以使用setDefaults(int)方法設定預設響應參數,在Notification中,對它的參數使用常量定義了,我們隻需使用即可:
         1:DEFAULT_ALL:鈴聲、閃光、震動均系統預設。
         2:DEFAULT_SOUND:系統預設鈴聲。
         3:DEFAULT_VIBRATE:系統預設震動。
         4:DEFAULT_LIGHTS:系統預設閃光。

         需要通路硬體裝置的話,是需要對其進行授權的,是以需要在清單檔案AndroidManifest.xml中增加兩個授權
         ,分别授予通路振動器與閃光燈的權限:

          閃光燈權限   <uses-permission android:name="android.permission.FLASHLIGHT"/>
          振動器權限    <uses-permission android:name="android.permission.VIBRATE"/>
         */
        builder.setDefaults(Notification.DEFAULT_ALL);


        /**
         * PendingIntent.FLAG_ONE_SHOT:  send()隻能被執行一次,即是說,假如該通知點選後不消失,那麼再次點選不會發生任何事。
         * PendingIntent.FLAG_UPDATE_CURRENT:  Extra會被更新為最後一個傳入的Intent的Extra
         * PendingIntent.FLAG_CANCEL_CURRENT:  這個,會取消之前的intent
         * PendingIntent.FLAG_NO_CREATE: 這個最好不用,不會建立
         */
        Intent intent=new Intent(this, IndexActivity.class);    //這裡寫好您要跳轉的activity

        PendingIntent pendingIntent=PendingIntent.getActivities(this,230,new Intent[]{intent},PendingIntent.FLAG_ONE_SHOT);

        builder.setContentIntent(pendingIntent);



        NotificationManager manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.notify(1,builder.build());   //發送通知

        //manager.cancelAll();清除通知
    }
}
           

第二種樣式==》大視圖

NotificationCompat.Builder builder = new NotificationCompat.Builder(
                this);
        builder.setContentTitle("大視圖來了");
        builder.setSmallIcon(R.drawable.ic_launcher);
//		builder.setContentText(">>>>");
        NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
        for (int i = 0; i < 10; i++) {
            style.addLine(">>>" + i);
        }
        builder.setStyle(style);
        NotificationManager manager =
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.notify(0, builder.build());
           

第三章樣式  ==》進度條通知  

NotificationCompat.Builder builder=new NotificationCompat.Builder(getApplicationContext());
        builder.setContentTitle("我是标題");  //設定标題

        builder.setSmallIcon(R.drawable.ic_launcher);  //設定圖示

        builder.setContentText("已下載下傳50%");  //設定内容
        builder.setProgress(100,50,true);    //可以開一個線程,在該線程中使用setProgress(進度值的最大值,目前進度值,false(是否流動)),在notify方法即時通知進度條

        NotificationManager manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.notify(1,builder.build());
           

接下來就是第四種樣式==》自定義通知

首先建立一個layout布局noti_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="sdbhfgshdfhsdgvf"/>

</LinearLayout>
           

注意:應該是你的R.layout.my_status_service 這個layout中使用了RemoteViews不支援的元件

在RemoteViews這種調用方式中,你隻能使用以下幾種界面元件:

Layout:

    FrameLayout, LinearLayout, RelativeLayout

Component:

  AnalogClock, Button, Chronometer, ImageButton, ImageView, ProgressBar, TextView, ViewFlipper, ListView, GridView, StackView, AdapterViewFlipper

使用示例代碼:

public class NotiFiActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_noti_fi);
        initView4();
    }


    private void initView4(){


        Notification  notification=new Notification();


        notification.icon=R.drawable.ic_launcher;
        RemoteViews views=new RemoteViews(getPackageName(),R.layout.noti_layout);   //建立自定義的view


        notification.contentView=views;   //使用setContent(content)方法,将RemoteViews對象加載到普通通知對象中
        NotificationManager manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.notify(1,notification);


    }
}
           

以上就是四種樣式的使用案例,新手有空多敲兩下就可以了

繼續閱讀