美文网首页
利用RemoteViews自定义Notification

利用RemoteViews自定义Notification

作者: A_Coder | 来源:发表于2016-11-05 16:20 被阅读0次

    RemoteViews具有View的结构,是一个远程View,可以在其他进程中显示。由于它可以跨进程显示,所以为了能够更新他的界面,RemoteViews提供一组基础的操作用于跨进程更新它的UI。它在更新UI的时候无法像Activity和Fragment那样直接更新,它是运行在SystemServer进程中。为了能够跨进程更新界面,RemoteViews提供了一些列可以跨进程更新UI的方法,内部有一些列的set方法,这些方法都是View的子集。

    protected void showRemoteViewsNotification() {
        Notification.Builder builder = new Notification.Builder(this);
        builder.setTicker("Hello RemotesViews!");// 收到通知的时候用于显示于屏幕顶部通知栏的内容
        builder.setSmallIcon(R.drawable.ic_launcher);// 设置通知小图标,在下拉之前显示的图标
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));// 拉下后显示的图标
        builder.setWhen(System.currentTimeMillis());
        builder.setOngoing(true);// 不能被用户关掉,会一直显示,如音乐播放等
        builder.setAutoCancel(true);// 自动取消
        builder.setOnlyAlertOnce(true);// 只alert一次
        builder.setDefaults(Notification.DEFAULT_ALL);
        mRemoteViews.setImageViewResource(R.id.logo, R.drawable.large_icon);
        mRemoteViews.setTextViewText(R.id.notify_title, "自定义view的title");
        mRemoteViews.setTextViewText(R.id.notify_content, "自定义view的内容");
        mRemoteViews.setTextViewText(R.id.notify_time, getCurrentTime());
        builder.setContent(mRemoteViews);
    
        Intent intent = new Intent(this, SecondActivity.class);
        intent.putExtra(SINGLE, REMOTE_VIEWS_NOTIFICATION);
        intent.setPackage(this.getPackageName());
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        builder.setContentIntent(pendingIntent);
        Notification notification = builder.build();
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(0, notification);
    }
    

    相关文章

      网友评论

          本文标题:利用RemoteViews自定义Notification

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