美文网首页
android 自定义通知栏

android 自定义通知栏

作者: 打死也不加班 | 来源:发表于2019-01-17 14:25 被阅读0次
image.png

如何使用自定义通知栏:

       //1.获取系统通知的管理者
         NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //2.初始化一个notification的对象
        Notification.Builder mBuilder =new Notification.Builder(this);
        //android 8.0 适配     需要配置 通知渠道NotificationChannel
        NotificationChannel b;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            b = new NotificationChannel("1","乱七八糟的其他信息",         NotificationManager. IMPORTANCE_MIN);
            mNotificationManager.createNotificationChannel(b);
            mBuilder.setChannelId("1");
        }
        //添加自定义视图  activity_notification 
        RemoteViews mRemoteViews = new RemoteViews(getPackageName(),R.layout.activity_notification);
        //主要设置setContent参数  其他参数 按需设置
        mBuilder.setContent(mRemoteViews);
        mBuilder.setSmallIcon(R.drawable.icon);
        mBuilder.setOngoing(true);
        mBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.icon));
        mBuilder.setAutoCancel(true);
        //更新Notification
        mNotificationManager.notify(1,mBuilder .build());

Notification 参数使用 参考:
http://www.cnblogs.com/kexing/p/8371051.html

自定义通知栏
仅支持FrameLayout、LinearLayout、RelativeLayout三种布局控件
AnalogClock、Chronometer、Button、ImageButton、ImageView、ProgressBar、TextView、ViewFlipper、ListView、GridView、StackView和AdapterViewFlipper这些显示控件
否则会引起ClassNotFoundException异常。

如何为通知栏添加点击事件:

流程:点击通知栏 发送广播 app接收广播做相应处理:

为通知栏绑定广播事件:

        Intent Intent = new Intent();
        //设置广播   
        Intent.setAction("xxxx.xxxx.xxx.xxx.xxx");
         //为控件绑定事件
        mRemoteViews.setOnClickPendingIntent(R.id.ll_notification, PendingIntent.getBroadcast(MainActivity.this, 10, Intent, PendingIntent.FLAG_UPDATE_CURRENT));

1.FLAG_CANCEL_CURRENT:如果AlarmManager管理的PendingIntent已经存在,那么将会取消当前的PendingIntent,从而创建一个新的PendingIntent.
2.FLAG_UPDATE_CURRENT:如果AlarmManager管理的PendingIntent已经存在,让新的Intent更新之前Intent对象数据,例如更新Intent中的Extras,另外,我们也可以在PendingIntent的原进程中调用PendingIntent的cancel ()把其从系统中移除掉
3.FLAG_NO_CREATE:如果AlarmManager管理的PendingIntent已经存在,那么将不进行任何操作,直接返回已经.
4.FLAG_ONE_SHOT:该PendingIntent只作用一次.在该PendingIntent对象通过send()方法触发过后,PendingIntent将自动调用cancel()进行销毁,那么如果你再调用send()方法的话,系统将会返回一个SendIntentException.

添加广播接收:

public static class NotificationReceiver extends BroadcastReceiver
    {

        @Override
        public void onReceive(Context context, Intent intent)
        {
            String action = intent.getAction();
            Log.e("====",action);
            switch (action)
            {
                case "xxxx.xxxx.xxx.xxx.xxx":
                    //相应处理
                    break;
            }
        }
    }

AndroidManifest:

<receiver android:name="xxxxx.xxx.x.x.main.MainActivity$NotificationReceiver">
       <intent-filter>
          <action android:name="xxxx.xxxx.xxx.xxx.xxx"/>
       </intent-filter>
</receiver>

相关文章

网友评论

      本文标题:android 自定义通知栏

      本文链接:https://www.haomeiwen.com/subject/jvzmdqtx.html