android Notification 的使用(转自他人)

最近一直在研究 android ,并一边研究一边作应用。其中遇到了把程序通知常驻在 Notification 栏,而且不能被 clear 掉(就像android QQ同样)的问题。通过研究实现了其功能,现把 Notification 的使用总结以下: 
html

Notification 的使用须要导入 3 个类java

?
1
2
3
import android.app.PendingIntent;
import android.app.NotificationManager;
import android.app.Notification;

代码示例及说明android

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);              
Notification n = new Notification(R.drawable.chat, "Hello,there!" , System.currentTimeMillis());            
n.flags = Notification.FLAG_AUTO_CANCEL;               
Intent i = new Intent(arg0.getContext(), NotificationShow. class );
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);          
//PendingIntent
PendingIntent contentIntent = PendingIntent.getActivity(
         arg0.getContext(),
         R.string.app_name,
         i,
         PendingIntent.FLAG_UPDATE_CURRENT);
                 
n.setLatestEventInfo(
         arg0.getContext(),
         "Hello,there!" ,
         "Hello,there,I'm john." ,
         contentIntent);
nm.notify(R.string.app_name, n);

下面依次对每一段代码进行分析:app

?
1
NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

建立 NotificationManager,其中建立的 nm 对象负责“发出”与“取消”  Notification。ide

?
1
2
Notification n = new Notification(R.drawable.chat, "Hello,there!" , System.currentTimeMillis());            
n.flags = Notification.FLAG_ONGOING_EVENT; 

建立 Notification ,参数依次为:icon的资源id,在状态栏上展现的滚动信息,时间。其中建立的 n 对象用来描述出如今系统通知栏的信息,以后咱们将会看到会在 n 对象上设置点击此条通知发出的Intent。布局

?
1
n.flags = Notification.FLAG_AUTO_CANCEL;

设置 n.flags 为 Notification.FLAG_AUTO_CANCEL ,该标志表示当用户点击 Clear 以后,可以清除该通知。this

?
1
2
Intent i = new Intent(arg0.getContext(), NotificationShow. class );
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);

建立一个Intent,该Intent使得当用户点击该通知后发出这个Intentspa

请注意,若是要以该Intent启动一个Activity,必定要设置 Intent.FLAG_ACTIVITY_NEW_TASK 标记。code

Intent.FLAG_ACTIVITY_CLEAR_TOP :若是在当前Task中,有要启动的Activity,那么把该Acitivity以前的全部Activity都关掉,并把此Activity置前以免建立Activity的实例htm

Intent.FLAG_ACTIVITY_NEW_TASK :系统会检查当前全部已建立的Task中是否有该要启动的Activity的Task,如有,则在该Task上建立Activity,若没有则新建具备该Activity属性的Task,并在该新建的Task上建立Activity。更多请参见 “ (转载)Android下Affinities和Task ”

?
1
2
3
4
5
6
//PendingIntent
PendingIntent contentIntent = PendingIntent.getActivity(
         arg0.getContext(),
         R.string.app_name,
         i,
         PendingIntent.FLAG_UPDATE_CURRENT);

PendingIntent 为Intent的包装,这里是启动Intent的描述,PendingIntent.getActivity 返回的PendingIntent表示,此PendingIntent实例中的Intent是用于启动 Activity 的Intent。PendingIntent.getActivity的参数依次为:Context,发送者的请求码(能够填0),用于系统发送的Intent,标志位。

其中 PendingIntent.FLAG_UPDATE_CURRENT  表示若是该描述的PendingIntent已存在,则改变已存在的PendingIntent的Extra数据为新的PendingIntent的Extra数据。

这里再简要说一下 Intent 与 PendingIntent 的区别:

Intent :意图,即告诉系统我要干什么,而后系统根据这个Intent作对应的事。如startActivity至关于发送消息,而Intent是消息的内容。

PendingIntent :包装Intent,Intent 是咱们直接使用 startActivity , startService 或 sendBroadcast 启动某项工做的意图。而某些时候,咱们并不能直接调用startActivity , startServide 或 sendBroadcast ,而是当程序或系统达到某一条件才发送Intent。如这里的Notification,当用户点击Notification以后,由系统发出一条Activity 的 Intent 。所以若是咱们不用某种方法来告诉系统的话,系统是不知道是使用 startActivity ,startService 仍是 sendBroadcast 来启动Intent 的(固然还有其余的“描述”),所以这里便须要PendingIntent。

?
1
2
3
4
5
n.setLatestEventInfo(
         arg0.getContext(),
         "Hello,there!" ,
         "Hello,there,I'm john." ,
         contentIntent);

设置显示在通知下拉框中的信息,参数依次为:Context,标题,内容,PendingIntent。

?
1
nm.notify(R.string.app_name, n);

启动Notification,参数依次为:在你的程序中标识Notification的id值(用来区分同一程序中的不一样Notifycation,若是程序中只有一个Notification那么这里随便你填什么均可以,不过类型必需要为int),要通知的Notification。

如何使本身的Notification像Android QQ同样能出如今 “正在运行的”栏目下面

其实很简单,只需设置Notification.flags = Notification.FLAG_ONGOING_EVENT;即可以了。

如何改变 Notification 在“正在运行的”栏目下面的布局

建立 RemoteViews 并赋给 Notification.contentView ,再把 PendingIntent 赋给 Notification.contentIntent 即可以了,如:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
PendingIntent contentIntent = PendingIntent.getActivity(
     arg0.getContext(),
     R.string.app_name,
     i,
     PendingIntent.FLAG_UPDATE_CURRENT);
             
RemoteViews rv = new RemoteViews(Main. this .getPackageName(), R.layout.notification_view);
rv.setImageViewResource(R.id.image, R.drawable.chat);
rv.setTextViewText(R.id.text, "Hello,there,I'm john." );
n.contentView = rv;
n.contentIntent = contentIntent;
 
nm.notify(R.string.app_name, n);

注意,若是使用了contentView,那么便不要使用Notification.setLatestEventInfo。若是setLatestEventInfo在赋给 Notification.contentView 的代码以后,那么contentView的效果将被覆盖,显示的即是 setLatestEventInfo 的效果;若是 setLatestEventInfo 在 Notification.contentView 的代码以前,那么显示的即是 Notification.contentView 的效果,也就是说无论你想要setLatestEventInfo 或 contentView 的自定义效果,请保证始终只有一句设置代码,由于在最后一句绑定的时候,以前的设置contentView或setLatestEventInfo的代码都是彻底没有必要的。

 

shougao result(source code):

 

public class NotifytestActivity extends Activity {
   
    private NotificationManager myNotificationManager;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        myNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
       
        Intent notifyIntent = new Intent(this, Notifynew.class);
        notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent appIntent = PendingIntent.getActivity(this, 0, notifyIntent, 0);
       
        Notification myNotification = new Notification();
        myNotification.icon = R.drawable.ic_launcher;
        myNotification.tickerText = "tickerText";
        myNotification.defaults = Notification.DEFAULT_SOUND;
       
        myNotification.setLatestEventInfo(this, "contenttitle", "contenttext", appIntent);
       
        myNotificationManager.notify(0, myNotification);
       
    }

public class Notifynew extends Activity {     @Override     protected void onCreate(Bundle savedInstanceState) {         // TODO Auto-generated method stub         super.onCreate(savedInstanceState);                 Toast.makeText(this, "new info", Toast.LENGTH_SHORT).show();         //        finish();     }    

相关文章
相关标签/搜索