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

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

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
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

【附】相关架构及资料

源码、笔记、视频。高级UI、性能优化、架构师课程、NDK、混合式开发(ReactNative+Weex)微信小程序、Flutter全方面的Android进阶实践技术,和技术大牛一起讨论交流解决问题。

image

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

【附】相关架构及资料

源码、笔记、视频。高级UI、性能优化、架构师课程、NDK、混合式开发(ReactNative+Weex)微信小程序、Flutter全方面的Android进阶实践技术,和技术大牛一起讨论交流解决问题。

[外链图片转存中…(img-FdjMOHf8-1713596894309)]

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值