The method setLatestEventInfo(Context, CharSequence, CharSequence, PendingIntent) from the type
2016-03-16 11:29
429 查看
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?
package com.example.mynotification; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.text.NoCopySpan.Concrete; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { Button m_Button1; TextView m_txtView; NotificationManager mNotificationManager; Notification mNotification; Intent mIntent; PendingIntent mPendingIntent; Context context; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); context = this; final Notification notification; mNotificationManager = (NotificationManager) this .getSystemService(NOTIFICATION_SERVICE); m_Button1 = (Button) this.findViewById(R.id.button1); // 点击通知时转移内容 mIntent = new Intent(MainActivity.this, MainActivity1.class); mPendingIntent = PendingIntent.getActivity(MainActivity.this, 0, mIntent, 0); notification = new Notification.Builder(context).setAutoCancel(true) .setContentTitle("qq正在运行").setContentText("qq,让交流更多方便") .setContentIntent(mPendingIntent) .setSmallIcon(R.drawable.ic_launcher) .setWhen(System.currentTimeMillis()).build(); m_Button1.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { mNotificationManager.notify(0, notification); } }); } }
低于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?
package com.example.mynotification; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.text.NoCopySpan.Concrete; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { Button m_Button1; TextView m_txtView; NotificationManager mNotificationManager; Notification mNotification; Intent mIntent; PendingIntent mPendingIntent; Context context; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); context = this; final Notification notification; mNotificationManager = (NotificationManager) this .getSystemService(NOTIFICATION_SERVICE); m_Button1 = (Button) this.findViewById(R.id.button1); // 点击通知时转移内容 mIntent = new Intent(MainActivity.this, MainActivity1.class); mPendingIntent = PendingIntent.getActivity(MainActivity.this, 0, mIntent, 0); notification = new Notification.Builder(context).setAutoCancel(true) .setContentTitle("qq正在运行").setContentText("qq,让交流更多方便") .setContentIntent(mPendingIntent) .setSmallIcon(R.drawable.ic_launcher) .setWhen(System.currentTimeMillis()).build(); m_Button1.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { mNotificationManager.notify(0, notification); } }); } }
package com.example.mynotification; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.text.NoCopySpan.Concrete; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { Button m_Button1; TextView m_txtView; NotificationManager mNotificationManager; Notification mNotification; Intent mIntent; PendingIntent mPendingIntent; Context context; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); context = this; final Notification notification; mNotificationManager = (NotificationManager) this .getSystemService(NOTIFICATION_SERVICE); m_Button1 = (Button) this.findViewById(R.id.button1); // 点击通知时转移内容 mIntent = new Intent(MainActivity.this, MainActivity1.class); mPendingIntent = PendingIntent.getActivity(MainActivity.this, 0, mIntent, 0); notification = new Notification.Builder(context).setAutoCancel(true) .setContentTitle("qq正在运行").setContentText("qq,让交流更多方便") .setContentIntent(mPendingIntent) .setSmallIcon(R.drawable.ic_launcher) .setWhen(System.currentTimeMillis()).build(); m_Button1.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { mNotificationManager.notify(0, notification); } }); } }
相关文章推荐
- easyui表格datagrid列宽自适应
- C# AllowDrop设置为True后拖放无效的问题
- Arduino EEPROM 的操作
- 使用AsyncDisplayKit提升UICollectionView和UITableView的滚动性能
- String,StringBuffer,StringBuild区别
- UIViewController的生命周期及iOS程序执行顺序
- Android studio b Error: No resource found that matches the given name (at 'src' with value '@dra...
- iOS UIlabel一些常用属性方法总结
- SystemUI之Airplane mode之分析
- 日志搜集、过滤及推送处理框架logstash及fluentd总结
- request和response中文乱码
- UIPageControl-页面控件
- String与CharSequence的区别与联系
- Unknown class ViewController in Interface Builder file问题解决
- Android Volley入门到精通:定制自己的Request
- iOS UI自动化测试与代码覆盖率
- UICollectionView不能拖拽或者刷新抖动
- ContentValues
- UILable 的字体加宽,倾斜
- UIBezierPath精讲