[Android] Notification和广播的简单使用
【摘要】
目录
🌳效果展示🌳分析🌳全部代码
🌳效果展示
点击发送广播,会弹出通知 点击清除通知图标会删除通知
🌳分析
动态注册广播接收器
MyReceiver my...
🌳效果展示
点击发送广播,会弹出通知
点击清除通知图标会删除通知
🌳分析
动态注册广播接收器
MyReceiver myReceiver=new MyReceiver();
IntentFilter intentFilter=new IntentFilter();
intentFilter.addAction("cn.edu.henu.sendMessage");
//这个Action自己选择一个字符串,最好带上包名,不容易重复
registerReceiver(myReceiver,intentFilter);
- 1
- 2
- 3
- 4
- 5
intent发送广播
Intent intent=new Intent("cn.edu.henu.sendMessage");
//这里的action要和注册广播接收器那里的action一致
intent.putExtra("info","动态注册");
sendBroadcast(intent);
- 1
- 2
- 3
- 4
广播接收器接收到广播后,自动调用MyReceiver的OnReceive方法
在该方法中写弹出通知的逻辑代码
// 创建NotificationManager
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
String channelID = "my_channel_ID";
String channelNAME = "my_channel_NAME";
int level = NotificationManager.IMPORTANCE_HIGH;
// 8.0以上必须先创建通道
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());
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
删除通知
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(101);
//注意上面notify的时候id也是101
- 1
- 2
- 3
🌳全部代码
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);
}
});
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
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
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
String channelID = "my_channel_ID";
String channelNAME = "my_channel_NAME";
int level = NotificationManager.IMPORTANCE_HIGH;
// 8.0以上必须先创建通道
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());
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
文章来源: blog.csdn.net,作者:开心星人,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/qq_55675216/article/details/124827356
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)