栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

[Android] Notification和广播的简单使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

[Android] Notification和广播的简单使用

目录
    • 效果展示
    • 分析
    • 全部代码


效果展示


点击发送广播,会弹出通知

点击清除通知图标会删除通知


分析

动态注册广播接收器

 MyReceiver myReceiver=new MyReceiver();
 IntentFilter intentFilter=new IntentFilter();
 intentFilter.addAction("cn.edu.henu.sendMessage"); 
 //这个Action自己选择一个字符串,最好带上包名,不容易重复
 registerReceiver(myReceiver,intentFilter);

intent发送广播

Intent intent=new Intent("cn.edu.henu.sendMessage");
//这里的action要和注册广播接收器那里的action一致
intent.putExtra("info","动态注册");
sendBroadcast(intent);

广播接收器接收到广播后,自动调用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());

删除通知

NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(101);
//注意上面notify的时候id也是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
        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());

    }
}

activity_main.xml




   
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/894023.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号