gpt4 book ai didi

android - 如何使用 RemoteViews 更新通知?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:22:03 24 4
gpt4 key购买 nike

我正在使用 RemoteViews 创建通知来自自定义 Service,它在前台模式下运行通知(也就是说,只要通知对用户可见,服务就会保持 Activity 状态)。通知设置为正在进行,因此用户无法将其关闭。

我想更改例如 ImageView 中显示的位图,包含在远程 View 的布局中或更改 TextView 中的文本值。远程 View 中的布局是使用 XML 布局文件设置的。

我的问题是,一旦创建通知并对用户可见,如果我调用任何 RemoteViews 的函数,如 setImageViewResource()要更改 ImageView 中显示的 Bitmap,更改是不可见的,除非我调用 setImageViewResource() 我之后调用:

NotificationManager.notify( id, notification );

Service.startForeground(id,notification);

虽然这对我来说听起来不对。我无法相信要在已创建的通知中更新 RemoteViews UI,我必须重新初始化通知。如果我在通知中有 Button 控件,它会在触摸和释放时自行更新。所以必须有一种方法可以正确地做到这一点,但我不知道怎么做。

这是我在 Service 实例中创建通知的代码:

this.notiRemoteViews = new MyRemoteViews(this,this.getApplicationContext().getPackageName(),R.layout.activity_noti1);

Notification.Builder notibuilder = new Notification.Builder(this.getApplicationContext());
notibuilder.setContentTitle("Test");
notibuilder.setContentText("test");
notibuilder.setSmallIcon(R.drawable.icon2);
notibuilder.setOngoing(true);

this.manager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
this.noti = notibuilder.build();
this.noti.contentView = this.notiRemoteViews;
this.noti.bigContentView = this.notiRemoteViews;
this.startForeground(NOTIFICATION_ID, this.noti);

以及“强制”UI 更改为通知的功能:

public void updateNotiUI(){
this.startForeground(NOTIFICATION_ID, this.noti);
}

MyRemoteViews 类中,需要时,我会这样做以更改 UI:

this.setImageViewResource(R.id.iconOFF, R.drawable.icon_off2);
this.ptMyService.updateNotiUI();

谁能告诉我在通知中更新 RemoteViews 的 UI 组件的正确方法是什么?

最佳答案

这是一个使用 RemoteViews 更新通知的详细示例:

private static final int NOTIF_ID = 1234;
private NotificationCompat.Builder mBuilder;
private NotificationManager mNotificationManager;
private RemoteViews mRemoteViews;
private Notification mNotification;
...

// call this method to setup notification for the first time
private void setUpNotification(){

mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

// we need to build a basic notification first, then update it
Intent intentNotif = new Intent(this, MainActivity.class);
intentNotif.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intentNotif, PendingIntent.FLAG_UPDATE_CURRENT);

// notification's layout
mRemoteViews = new RemoteViews(getPackageName(), R.layout.custom_notification_small);
// notification's icon
mRemoteViews.setImageViewResource(R.id.notif_icon, R.drawable.ic_launcher);
// notification's title
mRemoteViews.setTextViewText(R.id.notif_title, getResources().getString(R.string.app_name));
// notification's content
mRemoteViews.setTextViewText(R.id.notif_content, getResources().getString(R.string.content_text));

mBuilder = new NotificationCompat.Builder(this);

CharSequence ticker = getResources().getString(R.string.ticker_text);
int apiVersion = Build.VERSION.SDK_INT;

if (apiVersion < VERSION_CODES.HONEYCOMB) {
mNotification = new Notification(R.drawable.ic_launcher, ticker, System.currentTimeMillis());
mNotification.contentView = mRemoteViews;
mNotification.contentIntent = pendIntent;

mNotification.flags |= Notification.FLAG_NO_CLEAR; //Do not clear the notification
mNotification.defaults |= Notification.DEFAULT_LIGHTS;

// starting service with notification in foreground mode
startForeground(NOTIF_ID, mNotification);

}else if (apiVersion >= VERSION_CODES.HONEYCOMB) {
mBuilder.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(false)
.setOngoing(true)
.setContentIntent(pendIntent)
.setContent(mRemoteViews)
.setTicker(ticker);

// starting service with notification in foreground mode
startForeground(NOTIF_ID, mBuilder.build());
}
}

// use this method to update the Notification's UI
private void updateNotification(){

int api = Build.VERSION.SDK_INT;
// update the icon
mRemoteViews.setImageViewResource(R.id.notif_icon, R.drawable.icon_off2);
// update the title
mRemoteViews.setTextViewText(R.id.notif_title, getResources().getString(R.string.new_title));
// update the content
mRemoteViews.setTextViewText(R.id.notif_content, getResources().getString(R.string.new_content_text));

// update the notification
if (api < VERSION_CODES.HONEYCOMB) {
mNotificationManager.notify(NOTIF_ID, mNotification);
}else if (api >= VERSION_CODES.HONEYCOMB) {
mNotificationManager.notify(NOTIF_ID, mBuilder.build());
}
}

通知的布局,即 res/layout/custom_notification_small.xml:

<!-- We have to set the height to 64dp, this is the rule of the small notification -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="64dp"
android:orientation="horizontal"
android:id="@+id/notif_small"
android:background="@drawable/notification_background">

<ImageView
android:id="@+id/notif_icon"
android:contentDescription="@string/notif_small_desc"
android:layout_width="47dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:src="@drawable/ic_launcher"
android:layout_marginLeft="7dp"
android:layout_marginRight="9dp"/>

<TextView
android:id="@+id/notif_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/notif_icon"
android:singleLine="true"
android:paddingTop="8dp"
android:textSize="17sp"
android:textStyle="bold"
android:textColor="#000000"
android:text="@string/app_name"/>

<TextView
android:id="@+id/notif_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/notif_icon"
android:paddingBottom="9dp"
android:layout_alignParentBottom="true"
android:singleLine="true"
android:textSize="13sp"
android:textColor="#575757"
android:text="Content" />
</RelativeLayout>

希望这个例子对你有很大帮助!

注意:您无法在 pre-Honeycomb 上更新自定义 NotificationCompat,因此我添加了一种替代方法来在 pre-Honeycomb 上更新它,即首先检查 API 级别并使用已弃用的 Notification 代替。

关于android - 如何使用 RemoteViews 更新通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45087779/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com