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

1.Android RemoteViews 定义

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

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

学习目录:


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 布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="sendRecommendMsg"
        android:text="发送推荐消息" />

</LinearLayout>

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);
    }

效果图:

image.png

点击后:


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


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:orientation="vertical"
    android:paddingStart="48dp"
    android:paddingEnd="48dp">
       

    <ImageView
        android:id="@+id/icon"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_centerInParent="true"
        android:src="@drawable/ic_launcher_background" />
       

    <TextView
        android:id="@+id/tv_left"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:gravity="center_vertical"
        android:text="Left" />
       

    <TextView
        android:id="@+id/tv_right"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentEnd="true"
        android:gravity="center_vertical"
        android:text="Right" />
</RelativeLayout>



点击后效果:

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处理

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容