Android RemoteViews使用1-通知栏自定义布局

1.Android RemoteViews 定义

Android RemoteViews 可用在Notification中自定义通知栏布局。

因为Android 8.0创建通知栏的方式已经有所不同了。需要创建通知栏渠道,不然会报错,所以下面demo是以适配了Android 8.0通知栏来写的

学习目录:


Android RemoteViews使用1-通知栏自定义布局_第1张图片
image.png

1.Android 8.0通知栏适配

第一步:创建通知栏渠道:

  /**
     * 创建通知渠道
     *
     * @param channelId
     * @param channelName
     * @param importance
     */
    @TargetApi(Build.VERSION_CODES.O)
    private void createNotificationChannel(String channelId, String channelName, int importance) {
        NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
        NotificationManager notificationManager = (NotificationManager) getSystemService(
                NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(channel);
    }

第二步:在onCreate中调用创建通知栏渠道代码

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//如果手机版本是Android 8.0版本或者更高
        String channelId = "recommend";//渠道Id
           String channelName = "推荐消息";//渠道名,给用户看的
           int importance = NotificationManager.IMPORTANCE_DEFAULT;//重要等级默认
            createNotificationChannel(channelId, channelName, importance);//创建渠道
        }

第三步:发送通知:

xml 布局




    

Java代码:

 /**
     * 发送推荐通知
     *
     * @param view
     */
    public void sendRecommendMsg(View view) {

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification notification = new NotificationCompat.Builder(this, "recommend")
                .setContentTitle("收到一条推荐消息")
                .setContentText("全场9折抢购中!")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background))
                .setAutoCancel(true)
                .build();
        manager.notify(1, notification);
    }

效果图:

Android RemoteViews使用1-通知栏自定义布局_第2张图片
image.png

点击后:


Android RemoteViews使用1-通知栏自定义布局_第3张图片
image.png

2.RemoteViews 使用:

RemoteViews的一些Api

1.构造方法:

第一个参数是包名,第二个是要加载的RemoteViews的布局资源文件。
public RemoteViews(String packageName, int layoutId) {
this(getApplicationInfo(packageName, UserHandle.myUserId()), layoutId);
}

2.提供的方法
设置TextView的内容
public void setTextViewText (int viewId, CharSequence text)
传入控件id和需要设置的字

设置ImageView 的内容
setImageViewResource(int viewId, int srcId)
控件id和图片资源

点击事件处理
setOnClickPendingIntent(int viewId, PendingIntent pendingIntent)
控件id和跳转intent

3.实例:

第一步:自定义RemoteView 内容

    @NotNull
    private RemoteViews getRemoteViews() {
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.remoteview);//RemoteView传入布局
        remoteViews.setTextViewText(R.id.tv_left, "开始");//设置textView内容
        remoteViews.setTextViewText(R.id.tv_right, "跳转");//设置textView内容
        remoteViews.setImageViewResource(R.id.icon, R.drawable.ic_launcher);//设置图片样式
        remoteViews.setOnClickPendingIntent(R.id.tv_right, pendingIntent);//点击跳转事件
        return remoteViews;
    }



第二步:在通知栏的基础上增加对自定义布局的支持

  public void sendRecommendMsg(View view) {

        RemoteViews remoteViews = getRemoteViews();//创建自定义布局
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification notification = new NotificationCompat.Builder(this, "recommend")
                .setContentTitle("收到一条推荐消息")
                .setContentText("全场9折抢购中!")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background))
                .setAutoCancel(true)
                .setContent(remoteViews)//在这里设置自定义通知的内容
                .build();
        manager.notify(1, notification);
    
    }


RemoteViews 布局xml




       

    
       

    
       

    




点击后效果:

image.png

遇到的问题:
点击通知栏后通知无法消失:

改成如下:

Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews remoteViews = getRemoteViews();

Notification notification = new NotificationCompat.Builder(this, "chat")
        .setContentTitle("收到一条聊天消息")
        .setContentText("今天中午吃什么?")
        .setWhen(System.currentTimeMillis())
        .setSmallIcon(R.drawable.ic_launcher_background)
        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background))
        .setContentIntent(pendingIntent).setAutoCancel(true)
        .setContent(remoteViews)//在这里设置自定义通知的内容
        .build();
xiaoMiShortCut(NotificationRemoteViewActivity.this, 20, notification);
manager.notify(1, notification);//解决点击后不会消失问题


private RemoteViews getRemoteViews() {

        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.remoteview);//RemoteView传入布局
        remoteViews.setTextViewText(R.id.tv_left, "开始");//通过id-内容的方式设置remoteview中控件的内容,底层实现是通过Binder跨进程通信
        remoteViews.setTextViewText(R.id.tv_right, "跳转");
        remoteViews.setImageViewResource(R.id.icon, R.drawable.ic_launcher);//设置图片样式
        return remoteViews;
    }

不在 getRemoteViews 处理点击跳转事件,在setContentIntent处理

如果你觉得文章对你有帮助,就帮忙点个赞吧,如果想支持的话就打个赏吧。

你可能感兴趣的:(Android RemoteViews使用1-通知栏自定义布局)