Days 21 Notification & BroadcastReceiver

本文详细介绍了Android中Notification的构建和使用方法,包括兼容性处理、Notification的基本属性设置、PendingIntent的应用、自定义视图设置及进度显示。此外,还介绍了Android广播的基本原理及其发送与接收过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(一)Notification显示在手机状态栏的通知,是一种具有全局效果的通知,首先会表现为一个图标的形式,当用户向下滑动时,展示出通知具体的内容。
1、构建Notification
若考虑向下兼容的问题,一般推荐使用NotificationCompat.Builder创建(NotificationCompat类在v4包中);
如果不考虑向下兼容的问题,使用Notification.Builder构建
2、Notification.Manager类是一个通知管理器类,这个对象是由系统维护的服务,应用程序可以通过NotificationManager 向系统发送全局通知;是以单例模式获得,所以一般并不实例化这个对象。在Activity中通过Activity.getSystemService(String)方法通过Android系统级服务的句柄,返回对应的对象。在这里需要返回Notification.Manager,所以直接传递Context.NOTIFICATION_SERVICE即可。
manager的notify(int, Notification) 方法来启动Notification。第一个参数是该Notification唯一的标识ID,第二个参数是该Notification对象。因为Notification.Manager是全局的,所以在其它Activity中可以通过NotificationManager 的cancel(int)方法,来清除某个通知。其中参数就是Notification的唯一标识ID。也可以通过 cancelAll() 来清除状态栏所有的通知。
3、通知中有几个属性是必须要设置的:
小图标,使用setSmallIcon()方法设置
标题,使用setContentTitile()方法设置
文本内容,使用setContentText()方法设置
这里写图片描述
Demo1:

public class MainActivity extends AppCompatActivity {

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

    public void showNotification(View view) {
//        通过NotificationCompat的Builder构建一个Builder对象
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

//        给通知设置小图标,该设置必须有
        builder.setSmallIcon(R.mipmap.ic_launcher);

//        给通知设置默认的铃声和振动
        builder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);

//        给通知设置大图标,即上图左部
       builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.dot_enable));

//        设置通知的内容
        builder.setContentTitle("标题");
//        设置通知的内容文本
        builder.setContentText("i am content text.");
//        给通知设置内容信息
        builder.setContentInfo("i am content infomation.");
//        设置提示性的文字(会在当前界面顶部短暂显示一个提示框)
        builder.setTicker("i am ticker");

//        通过Builder对象构建Notification对象
        Notification notification = builder.build();

//        通过getSystemService()方法获取NotificationManager对象
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

//        通过NotificationManager对象的notify()方法正式发送通知
//        第一个参数唯一的标识该Notification,第二个参数就是Notification对象。
        manager.notify(1, notification);

    }
}

Demo2:


//        通知栏显示多行文字内容会把ContentText覆盖掉
//        builder.setContentText("i am contenttext");

//        给通知设置“大格式”,通过InboxStyle.addLine()方法,可以在通知栏中显示多行文字内容(IndexStyle静态内部类)
        NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();

        for (int i = 0;i<5;i++){
            style.addLine("i am line"+i);
        }

//        将样式设置给了builder对象
        builder.setStyle(style);

4、PendingIntent
PendingIntent是一种特殊的Intent,实际上就是对Intent的包装。主要的区别在于Intent是立刻执行,而PendingIntent是条件满足才发送企图,而且PendingIntent也可以取消。相比于Intent的优势在于自己携带有Context对象,这样它就不必依赖于Activity才可以存在。
得到PendingIntent对象的方法:
PendingIntent的静态方法,getActivity(context,int,intent,int)
主要的使用的地方和例子:通知Notification的发送,短消息SmsManager的发送和警报器AlarmManager的执行等。
面试:
Intent和PendingIntent的区别:
a、 Intent是立即使用的,而PendingIntent可以等到事件发生后触发,PendingIntent可以cancel;
b、 Intent在程序结束后即终止,而PendingIntent在程序结束后依然有效;
c、PendingIntent自带Context,而Intent需要在某个Context内运行;
d、Intent在原task中运行,PendingIntent在新的task中运行。
Demo3:

//        通过PendingIntent的静态方法getActivity()得到PendingIntent实例,包含四个参数:
//        1、context  2、发送通知的请求码     3、intent  包含要跳转到的目标Activity的intent  4、通知显示的行为
//        (为了避免影响之前的消息,requestCode每次要设置不同的内容)
        PendingIntent pi = PendingIntent.getActivity(MainActivity.this,2,intent,PendingIntent.FLAG_ONE_SHOT);

//        可以通过builder的setContentIntent()方法放置目标意图(PendingIntent)
        builder.setContentIntent(pi);

// 静态方法第四个参数的几个常量:
FLAG_ONE_SHOT : 这个PendingIntent只能使用一次。
FLAG_NO_CREATE : 如果被描述的PendingIntent不存在,那么简单地返回null,而不是创建它。
FLAG_CANCEL_CURRENT : 如果被描述的PendingIntent已经存在,在即将生成一个新的PendingIntent前,当前的一个要被取消。
FLAG_UPDATE_CURRENT :如果被描述的PendingIntent已经存在,那么继续保持它,但它其中的数据会因为新Intent而更新。
5、设置自定义视图RemoteViews
Demo4:

//        设置自定义视图,会把上面设置的普通视图覆盖掉,可以实现跨进程应用程序的访问包含两个参数
//        1、包名 通过getPackageName()方法获得  2、自定义视图的布局文件id
        RemoteViews views = new RemoteViews(getPackageName(),R.layout.play_item);

        Intent intent = new Intent(this,PlayActivity.class);

        PendingIntent pi = PendingIntent.getActivity(this,1,intent,PendingIntent.FLAG_ONE_SHOT);

//        给RemoteViews设置点击的目标意图;包含两个参数
//        1、在指定id的控件上点击后才会跳转到目标Activity  2、PendingIntent
        views.setOnClickPendingIntent(R.id.ivPlay,pi);

//        错误过:这行代码会给整个通知栏设置PendingIntent即等待意图,即Demo3中方法
//        只要在通知栏里点击就会跳转到pi中的目标Activity,而我们只想要在R.id.ivPlay控件上点击才会跳转
//        builder.setContentIntent(pi);

//        通过Builder对象的setContent(RemoteViews),将自定义视图RemoteViews放入通知栏中
        builder.setContent(views);

6、设置进度
通过Builder的setProgress(max,progress,boolean)方法设置进度,包含三个参数:1、max 进度的最大值 2、当前进度 3、true 循环流动的,无明确样式的进度条样式;false 有进度刻度的
Demo5:

new Thread(){
            @Override
            public void run() {
                super.run();
                for (int i = 0;i<10;i++){
//                    通过Builder的setProgress()方法设置进度,包含三个参数
//                    1、max 进度的最大值 2、当前进度 3、true 循环流动的,无明确进度的进度条样式;false 有进度刻度的
                    builder.setProgress(100,i,true);
//                    通过通知管理器对象的notify方法将通知循环发送
                    manager.notify(1,builder.build());

                    SystemClock.sleep(1000);
                }
//模拟下载完成,这里进度条样式要设置为false,进度及进度最大值均为0
      builder.setProgress(0,0,false).setContentText("done");

                manager.notify(1,builder.build());
            }
        }.start();

(二)广播
备注:
1、广播能够通过sendBroadcast(Intent)方法将数据发送给各个”各个客户端”BroadReceiver
2、作为接收端必须新增类继承自BroadcastReceiver,并且实现该抽象类中的抽象方法,onReceive(Context,Intent)
3、发送广播
广播的发送方只管发送【sendBroadcast(Intent)】,不管是否有客户端接收

        Intent intent = new Intent();

//        给Intent设置感兴趣的“频段”(中间不能有空格)
        intent.setAction("sbcomputer");
//        Intent准备需要传递的数据
        intent.putExtra("info","I like android!");

//        发送广播
        sendBroadcast(intent);

4、接收广播
receiver可以看/改广播信息里的内容
广播接收必须要注册:两种方式
a、静态注册
1、在AndroidManifest.xml文件中新增receiver节点,定义name属性,指向要注册的Receiver,在receiver节点下增加intent-filter节点,在intent-filter节点下添加action节点,来指明接收方要接受的“频段”,此频段对应发送广播的intent的setAction(“频段”)
2、通过intent-filter的priority属性设置优先级,该值越大越先被触发,范围从-1000到1000,默认为零,优先级一样则先注册就先触发

<receiver
            android:name=".MyReceiver1"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:priority="100">
                <action android:name="sbcomputer" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>

b、动态注册

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值