字节跳动 java 开源商城系统 pandas xcode tsql flash redis常用语句 menu electron Amaze UI vue实例 sql server 视频教程 android经典项目开发实战 jquery点击事件 mysql操作日志 安卓小程序源码 python编程练习题 vim跳到文件末尾 kubernetes视频教程 python课程 python安装模块 java抽象类 java在线课程 java基础代码 java接口实例 sql实例 广告代码 xp系统修复 popen 微信助力软件 网络是怎样连接的 只狼鬼佛 电脑基础 bz2解压命令 0x00000057 扫微信二维码诈骗原理 php取整 脚本列表 跑马灯动态壁纸
当前位置: 首页 > 学习教程  > 编程语言

安卓:在广播接收器中创建通知

2020/11/24 9:18:42 文章标签: 测试文章如有侵权请发送至邮箱809451989@qq.com投诉后文章立即删除

通知的用法比较灵活,分别可以在三种情景下创建:可以在activity里创建,可以在BroadcastReceiver里创建,也可以在Service里创建,不过在activity里创建比较少用,因为一般只有当程序进入后台的时候才需要使用通…

通知的用法比较灵活,分别可以在三种情景下创建:可以在activity里创建,可以在BroadcastReceiver里创建,也可以在Service里创建,不过在activity里创建比较少用,因为一般只有当程序进入后台的时候才需要使用通知。

效果看起来都差不多:

在这里插入图片描述
代码实现-------------------------------------------------------------

1.ActivityCollector.java

public class ActivityCollector {

    public static List<Activity> activities = new ArrayList<>();

    public static void addActivity(Activity activity){
        activities.add(activity);
    }

    public static void removeActivity(Activity activity){
        activities.remove(activity);
    }

    public static void finishAll(){
        for(Activity activity: activities){
            if(!activity.isFinishing()){
                activity.finish();
            }
        }
    }
}

2.BaseActivity.java

public class BaseActivity extends AppCompatActivity {

    private NotificationReceiver notificationReceiver;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityCollector.addActivity(this);
    }

    @Override
    protected void onResume() {
        super.onResume();

        IntentFilter intentFilter_notice = new IntentFilter();
        intentFilter_notice.addAction("com.example.broadcastbestpractice.NOTICE_TEST");
        notificationReceiver = new NotificationReceiver();
        registerReceiver(notificationReceiver, intentFilter_notice);
    }

    @Override
    protected void onPause() {
        super.onPause();

        if(notificationReceiver != null){
            unregisterReceiver(notificationReceiver);
            notificationReceiver = null;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.removeActivity(this);
    }

    class NotificationReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(final Context context, Intent intent) {
            //获取通知管理实例
            NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

            //8.0一张版本判断
            if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
                NotificationChannel channel=new NotificationChannel("important","Important",NotificationManager.IMPORTANCE_HIGH);
                assert manager != null;
                manager.createNotificationChannel(channel);
            }
            //通知点击事项
            Intent intent1=new Intent(context,NotificationActivity.class);
            PendingIntent pendingIntent=PendingIntent.getActivity(context,0,intent1,0);

            Notification notification= new NotificationCompat.Builder(context,"important")
                    .setContentTitle("收到一条通知")
                    .setContentText("你好")
                    .setSmallIcon(R.mipmap.ic_launcher)//通知图标
                    .setContentIntent(pendingIntent)//点击跳到通知详情
                    .setAutoCancel(true)//当点击通知后显示栏的通知不再显示
                    .build();
            assert manager != null;
            manager.notify(1,notification);

        }
    }
}

3.MainActivity.java

public class MainActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button send_notice=findViewById(R.id.send_notice);

        send_notice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("com.example.broadcastbestpractice.NOTICE_TEST");
                sendBroadcast(intent);
            }
        });
    }
}

4.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.example.broadcastbestpractice.MainActivity">

    <Button
        android:layout_centerInParent="true"
        android:id="@+id/send_notice"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:text="发送通知"/>

</RelativeLayout>

5.NotificationActivity.java

public class NotificationActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification);
    }
}

6.activity_notification.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".NotificationActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="24sp"
        android:text="这是通知的具体内容"/>

</RelativeLayout>

GitHub:https://github.com/wangjinchan/BroadcastBestPractice1

参考:郭霖的《第一行代码》第三版kotlin


本文链接: http://www.dtmao.cc/news_show_400017.shtml

附件下载

相关教程

    暂无相关的数据...

共有条评论 网友评论

验证码: 看不清楚?