🌳效果展示
点击发送广播,会弹出通知 点击清除通知图标会删除通知
🌳分析
动态注册广播接收器
MyReceiver myReceiver=new MyReceiver();
IntentFilter intentFilter=new IntentFilter();
intentFilter.addAction("cn.edu.henu.sendMessage");
registerReceiver(myReceiver,intentFilter);
intent发送广播
Intent intent=new Intent("cn.edu.henu.sendMessage");
intent.putExtra("info","动态注册");
sendBroadcast(intent);
广播接收器接收到广播后,自动调用MyReceiver的OnReceive方法 在该方法中写弹出通知的逻辑代码
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
String channelID = "my_channel_ID";
String channelNAME = "my_channel_NAME";
int level = NotificationManager.IMPORTANCE_HIGH;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelID, channelNAME, level);
manager.createNotificationChannel(channel);
}
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context, channelID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Notification")
.setContentText("我是通知")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_CALL)
.setAutoCancel(true);
manager.notify(101, builder.build());
删除通知
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(101);
🌳全部代码
MainActivity.java
public class MainActivity extends AppCompatActivity {
private Button send;
private Button clear;
public Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyReceiver myReceiver=new MyReceiver();
IntentFilter intentFilter=new IntentFilter();
intentFilter.addAction("cn.edu.henu.sendMessage");
registerReceiver(myReceiver,intentFilter);
send=findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent("cn.edu.henu.sendMessage");
intent.putExtra("info","动态注册");
sendBroadcast(intent);
}
});
clear=findViewById(R.id.clear);
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(101);
}
});
}
}
MyReceiver.java
public class MyReceiver extends BroadcastReceiver {
private static final String TAG = "MyReceiver";
@Override
public void onReceive(Context context, Intent intent) {
String msg=intent.getStringExtra("info");
Toast.makeText(context,msg,Toast.LENGTH_LONG).show();
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
String channelID = "my_channel_ID";
String channelNAME = "my_channel_NAME";
int level = NotificationManager.IMPORTANCE_HIGH;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelID, channelNAME, level);
manager.createNotificationChannel(channel);
}
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context, channelID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Notification")
.setContentText("2012080097_张开斌")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_CALL)
.setAutoCancel(true);
manager.notify(101, builder.build());
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送广播消息"
/>
<Button
android:id="@+id/clear"
android:text="清除通知图标"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
|