- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在使用 RemoteViews
创建通知来自自定义 Service
,它在前台模式下通过通知运行(也就是说,只要通知对用户可见,服务就会保持 Activity 状态)。通知设置为进行中,因此用户无法将其关闭。
我想更改远程 View 布局中包含的 ImageView
中显示的位图,或者更改 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 级别并使用已弃用的 通知
代替。
关于android - 如何使用 RemoteViews 更新通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22789588/
我为 Android 应用程序创建小部件(当然是用 Java)。我有从布局(使用布局 ID)创建的经典 RemoteViews RemoteViews rv = new RemoteViews(con
我一直在研究如何使用 RemoteView 创建自定义布局通知。 到目前为止,我能够使用 contentView 和 bigContentView 指向带有自定义布局 xml 的 RemoteView
我使用两个应用程序。一个通过 AIDL 接口(interface)公开一个 RemoteView。第二个使用 ListView 和自定义适配器来呈现 RemoteView。 使用一个布局和一个 Tex
我正在开发两个 android 应用程序,一个是 baseApp,另一个是 pluginApp。目的是在安装相应插件时扩展 baseApp 的功能。 pluginApp 将包含 XML 布局文件,其中
我正在尝试使用自定义远程 View 创建媒体播放器控件通知。我遇到的问题是,每次我在远程 View 中更改某些内容时,我都需要取消通知并从头开始重建它。这种方法对性能非常不利,因为它明显滞后。 (即将
我在更新服务中的自定义通知值时遇到问题。我使用 RemoteView 并希望每秒更新一次 textview,但我没有任何真正的想法来做这件事。这是我的代码: int icon = R.drawable
整个早上都在努力尝试放置文本“正在模拟”下的进度条。谁能帮帮我?添加图片: 最佳答案 如果您不想按照其他答案的建议使用 RelativeLayout,这里有一个选项。
我一直在到处搜索 RemoteViews 中支持的方法列表,但一无所获。任何人都可以链接资源或列出 RemoteViews 支持的所有可用方法吗?谢谢。 最佳答案 我假设您指的是 RemoteView
到目前为止,我一直使用 remoteViews.setImageViewBitmap() 直接将位图加载到我的 RemoteViews 中。总的来说它工作正常。 但是一些用户遇到了问题,我认为是在加载
我正在创建一个使用一些小部件的应用程序,我在 remoteView 上使用 setInt() 方法来更改背景源。这不是问题,但我想知道有关方法名称的文档在哪里。在 RemoteViews API 中,
来自 the documentation , RemoteViews 的构造函数是 public RemoteViews (String packageName, int layoutId) 文档说
我正在通过集成 RemoteViews 实现自定义推送通知。问题是,remoteview 中的按钮没有显示。我没有弄错我做错了什么。 代码: public class AlarmReceiver ex
我的 Assets 文件夹中有一些 SVG,我需要在我的小部件(在 ImageView 上)中动态设置它们。 我正在使用这个库:http://code.google.com/p/svg-android
所以,经过大量的摸索之后,我已经束手无策了。我的通知中有一个媒体播放器 RemoteViews,我希望能够访问播放、暂停、上一个和下一个按钮。 我知道 setOnClickPendingIntent(
Jelly Bean 中引入的扩展通知允许添加操作。这些操作显示为一行中的按钮。如果我想用我自己的自定义 View 修改它,我会考虑远程 View 。 扩展通知是否允许远程查看?在扩展通知存在之前,我
我想将 TextView 的高度远程设置为 WRAP_CONTENT。 我该怎么做,因为没有 remoteviews.setLayoutParams(viewId, param) 函数? 我尝试了 r
我正在尝试将值转发到 Android RemoteView。一些值可以通过使用反射的 set 方法转发。例如背景颜色作品: rv.setInt(R.id.viewId, "setBackgroundC
我尝试创建自定义通知,但收到以下异常: FATAL EXCEPTION: main android.app.RemoteServiceException: Bad notification poste
这个问题在这里已经有了答案: Is there a way to animate on a Home Widget? (5 个回答) 关闭6年前。 将动画应用于 RemoteViews 内的 View
我有问题。有时我的服务会被这个 logcat 强行关闭: 03-26 20:44:44.849: E/AndroidRuntime(12080): FATAL EXCEPTION: main 03-2
我是一名优秀的程序员,十分优秀!