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

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=“发送通知”/>

5.NotificationActivity.java

Android核心知识点

面试成功其实是必然的,因为我做足了充分的准备工作,包括刷题啊,看一些Android核心的知识点,看一些面试的博客吸取大家面试的一些经验。

下面这份PDF是我翻阅了差不多3个月左右一些Android大博主的博客从他们那里取其精华去其糟泊所整理出来的一些Android的核心知识点,全部都是精华中的精华,我能面试到现在2-2资深开发人员跟我整理的这本Android核心知识点有密不可分的关系,在这里本着共赢的心态分享给各位朋友。

不管是Android基础还是Java基础以及常见的数据结构,这些是无原则地必须要熟练掌握的,尤其是非计算机专业的同学,面试官一上来肯定是问你基础,要是基础表现不好很容易被扣上基础不扎实的帽子,常见的就那些,只要你平时认真思考过基本上面试是没太大问题的。

最后为了帮助大家深刻理解Android相关知识点的原理以及面试相关知识,这里放上我搜集整理的2019-2021BAT 面试真题解析,我把大厂面试中常被问到的技术点整理成了PDF,包知识脉络 + 诸多细节。

节省大家在网上搜索资料的时间来学习,也可以分享给身边好友一起学习。

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

边好友一起学习。

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 14
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值