The method setLatestEventInfo(Context, CharSequence, CharSequence, PendingIntent) from the type

来源:互联网 发布:学编程的app有哪些 编辑:程序博客网 时间:2024/05/17 04:23
notification.setLatestEventInfo(context, title, message, pendingIntent);     不建议使用




低于API Level 11版本,也就是Android 2.3.3以下的系统中,setLatestEventInfo()函数是唯一的实现方法。 

Intent  intent = new Intent(this,MainActivity);  
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);  
notification.setLatestEventInfo(context, title, message, pendingIntent);          
manager.notify(id, notification);  

    高于API Level 11,低于API Level 16 (Android 4.1.2)版本的系统中,可使用Notification.Builder来构造函数。但要使用getNotification()来使notification实现。前面版本在notification中设置的Flags,icon等属性都已经无效,要在builder里面设置。
Notification.Builder builder = new Notification.Builder(context)  
            .setAutoCancel(true)  
            .setContentTitle("title")  
            .setContentText("describe")  
            .setContentIntent(pendingIntent)  
            .setSmallIcon(R.drawable.ic_launcher)  
            .setWhen(System.currentTimeMillis())  
            .setOngoing(true);  
notification=builder.getNotification();  

    高于API Level 16的版本,就可以用Builder和build()函数来配套的方便使用notification了。
Notification notification = new Notification.Builder(context)    
         .setAutoCancel(true)    
         .setContentTitle("title")    
         .setContentText("describe")    
         .setContentIntent(pendingIntent)    
         .setSmallIcon(R.drawable.ic_launcher)    
         .setWhen(System.currentTimeMillis())    
         .build();   
我的实例:
[java] view plain copy print?
  1. package com.example.mynotification;  
  2.   
  3. import android.app.Activity;  
  4.   
  5. import android.app.Notification;  
  6.   
  7. import android.app.NotificationManager;  
  8.   
  9. import android.app.PendingIntent;  
  10.   
  11. import android.content.Context;  
  12. import android.content.Intent;  
  13.   
  14. import android.os.Bundle;  
  15.   
  16. import android.text.NoCopySpan.Concrete;  
  17. import android.view.View;  
  18.   
  19. import android.widget.Button;  
  20.   
  21. import android.widget.TextView;  
  22.   
  23. public class MainActivity extends Activity {  
  24.   
  25.     Button m_Button1;  
  26.   
  27.     TextView m_txtView;  
  28.   
  29.     NotificationManager mNotificationManager;  
  30.   
  31.     Notification mNotification;  
  32.   
  33.     Intent mIntent;  
  34.   
  35.     PendingIntent mPendingIntent;  
  36.     Context context;  
  37.   
  38.     /** Called when the activity is first created. */  
  39.   
  40.     @Override  
  41.     public void onCreate(Bundle savedInstanceState) {  
  42.   
  43.         super.onCreate(savedInstanceState);  
  44.   
  45.         setContentView(R.layout.activity_main);  
  46.         context = this;  
  47.         final Notification notification;  
  48.         mNotificationManager = (NotificationManager) this  
  49.                 .getSystemService(NOTIFICATION_SERVICE);  
  50.   
  51.         m_Button1 = (Button) this.findViewById(R.id.button1);  
  52.   
  53.         // 点击通知时转移内容  
  54.   
  55.         mIntent = new Intent(MainActivity.this, MainActivity1.class);  
  56.   
  57.         mPendingIntent = PendingIntent.getActivity(MainActivity.this0,  
  58.                 mIntent, 0);  
  59.   
  60.         notification = new Notification.Builder(context).setAutoCancel(true)  
  61.                 .setContentTitle("qq正在运行").setContentText("qq,让交流更多方便")  
  62.                 .setContentIntent(mPendingIntent)  
  63.                 .setSmallIcon(R.drawable.ic_launcher)  
  64.                 .setWhen(System.currentTimeMillis()).build();  
  65.   
  66.         m_Button1.setOnClickListener(new Button.OnClickListener() {  
  67.   
  68.             public void onClick(View v) {  
  69.   
  70.                 mNotificationManager.notify(0, notification);  
  71.   
  72.             }  
  73.         });  
  74.   
  75.     }  
  76. }  

0 0