android通知Notification
来源:互联网 发布:淘宝买家恶意退货 编辑:程序博客网 时间:2024/06/03 19:23
通知可以在活动,内容提供器,服务,广播接收器里创建。
1、调用 Context的getSystemService(Context.NOTIFICATION_SERVICE)方NotificationManager 来对通知进行管理
2、创建Notification对象,存储通知的各种信息
3、Intent:倾向立即执行某个动作
PendingIntent :倾向某个时机去执行某个动作,延时执行
静态方法获取不同类型的请求:getActivity(),getBroadcast(),getService()
4、设定通知的布局,调用
Notification 的 setLatestEventInfo()方法
5、调用 NotificationManager 的 notify()方法显示通知
NotificationActivity
public class NotificationActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.notification_layout); NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); manager.cancel(1);//取消通知 }}
public class MainActivity extends Activity implements OnClickListener { private Button sendNotice; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sendNotice = (Button) findViewById(R.id.send_notice); sendNotice.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.send_notice: NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Notification notification = new Notification( R.drawable.ic_launcher, "This is ticker text", System.currentTimeMillis()); //Uri soundUri = Uri.fromFile(new File("/system/media/audio/ringtones/Basic_tone.ogg")); //notification.sound = soundUri; //long[] vibrates = {0, 1000, 1000, 1000}; //notification.vibrate = vibrates;//震动 //notification.ledARGB = Color.GREEN;//led灯 //notification.ledOnMS = 1000; //notification.ledOffMS = 1000; //notification.flags = Notification.FLAG_SHOW_LIGHTS; notification.defaults = Notification.DEFAULT_ALL; //想要启动NotificationActivity Intent intent = new Intent(this, NotificationActivity.class); //将构建好的 Intent 对象传入到 PendingIntent 的 getActivity()方法里 PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); //布局 notification.setLatestEventInfo(this, "This is content title", "This is content text", pi); //显示通知 manager.notify(1, notification); break; default: break; } }}
结果:
0 0
- Notification---Android Notification通知详解
- 定制android通知(Notification)
- 定制android通知(Notification)
- android之Notification通知
- android之Notification通知
- Android 消息通知-Notification
- Android Notification通知详解
- android之Notification通知
- Android通知Notification
- Android 通知栏 Notification
- Android Notification通知详解
- android之Notification通知
- Android 状态栏通知Notification
- android之Notification通知
- android之Notification通知
- Android Notification通知详解
- Android之Notification通知
- android之Notification通知
- 程序员生存定律
- java.io
- 查看存储在iPhone Simulator沙盒中应用数据的方法
- day19/装饰和继承.java
- JVM实用参数(三)打印所有XX参数及值
- android通知Notification
- 浅谈NSRunLoop
- Container With Most Water
- traceroute
- java.io
- C++对于表达式临时对象的处理
- python sqlite 读取数据
- 比较运算符的结果一定是boolean类型
- day20/FileDemo1.java