public class MainActivity extends Activity {
NotificationManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final List<Integer> nId = new ArrayList<Integer>(Arrays.asList(1,2,3,4,5,6,7,8,9));
final List<Integer> cId = new ArrayList<>();
Button send = (Button) findViewById(R.id.send);
Button cancel = (Button) findViewById(R.id.cancel);
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
android.util.Log.d("notification", "nId=" + nId.get(0));
send.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
String id = "channel_1";
String description = "143";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel channel = new NotificationChannel(id, description, importance);
manager.createNotificationChannel(channel);
Notification notification = new Notification.Builder(MainActivity.this, id)
.setCategory(Notification.CATEGORY_MESSAGE)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("通知标题" + nId.get(0))
.setContentText("通知摘要")
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.build();
manager.notify(nId.get(0), notification);
cId.add(nId.get(0));
nId.remove(0);
}
});
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!cId.isEmpty()) {
manager.cancel(cId.get(0));
cId.remove(0);
}
}
});
}
}
android 下拉通知栏--通知的发送取消测试Demo
最新推荐文章于 2023-04-29 15:47:43 发布