本示例说明如何创建一个简单的通知,当用户单击该通知时启动该应用程序。
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) // 通知图标 .setContentTitle("Simple notification") // 标题 .setContentText("Hello word") // 身体信息 .setAutoCancel(true); // 单击时清除通知
Intent intent = new Intent(this, MainActivity.class); PendingIntent pi = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK); mBuilder.setContentIntent(pi);
NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify(0, mBuilder.build());