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

通知的用法比较灵活,分别可以在三种情景下创建:可以在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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一粒程序米

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值