Notification的使用

来源:互联网 发布:js如何获取当前时间 编辑:程序博客网 时间:2024/05/24 05:10

public class MainActivity extends Activity implements OnClickListener{private Button send;private Button cancel;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_noti);send = (Button)findViewById(R.id.btn_send);cancel = (Button)findViewById(R.id.btn_cancel);send.setOnClickListener(this);cancel.setOnClickListener(this);}@Overridepublic void onClick(View v) {if (v.getId() == R.id.btn_send) {send();} else {cancel();}}private int id = 1325353;private int id2 = 1325353;private int i = 0;private void send() {Builder builder = new Notification.Builder(this);builder.setSmallIcon(R.drawable.ic_launcher);/** * 状态栏的提示 */builder.setTicker("您有了一条新的消息");builder.setContentTitle("coding for fun!" + i++);builder.setWhen(System.currentTimeMillis());builder.setContentText("你好");builder.setNumber(id);Intent my = new Intent(this, com.robot.ok315.MainActivity.class);PendingIntent intent = PendingIntent.getActivity(this, 0, my, PendingIntent.FLAG_NO_CREATE);builder.setContentIntent(intent);builder.setDefaults(Notification.DEFAULT_SOUND);builder.setDefaults(Notification.DEFAULT_LIGHTS); // 需要设置权限builder.setDefaults(Notification.DEFAULT_VIBRATE); // 需要设置权限// builder.setDefaults(Notification.DEFAULT_VIBRATE); // 设置声音,指示灯,震动NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);notificationManager.notify(id, builder.getNotification());}private void cancel() {NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);notificationManager.cancel(id2);}}



注意:如果Notification的id相同,则只会生成一条通知信息。

0 0
原创粉丝点击