1. 效果图
2. 实现代码
public class firstActivity extends Activity { private Notification notification; // Notification对象 private NotificationManager notificationManager; // NotificationManager对象 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btn1 = (Button) findViewById(R.id.button1); // 获取按钮对象 Button btn2 = (Button) findViewById(R.id.button2); // 获取按钮对象 String service = NOTIFICATION_SERVICE; // 获取系统服务 notificationManager = (NotificationManager) getSystemService(service); notification = new Notification(); // 实例化一个Notification notification.icon = R.drawable.icon; // 指定图标 notification.tickerText = "Notification测试 "; // 指定提示信息 notification.when = System.currentTimeMillis(); // 指定显示的时间 btn1.setOnClickListener(new View.OnClickListener() { // 设置监听器 @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(firstActivity.this, firstActivity.class); // PendingIntent 相当于intent 的包装类 --> 表示即将到来的事情 PendingIntent pendingIntent = PendingIntent.getActivity( firstActivity.this, 0, intent, 0); notification.setLatestEventInfo(firstActivity.this, "我的标题", "我的内容", pendingIntent); notificationManager.notify(1, notification); // 发出通知 } }); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub notificationManager.cancel(1); // 取消通知 } }); } }
firstActivity
时间: 2024-10-06 00:36:04