我一直在研究如何使用RemoteView
创建自定义布局的通知。
到目前为止,我已经能够创建一个通知,并且将contentView
和bigContentView
指向包含自定义布局xml的RemoteView
。但是,当创建这个RemoteView
时,与自定义布局相关联的Activity
并没有被启动。
我已经仔细检查了我的布局xml文件,在其中看起来有正确的Activity
类名:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
tools:context=".LLMNotificationActivity" >
..... the rest are standard layout items: images, buttons and text
</RelativeLayout>
在清单文件中,在主应用程序主活动之后,还会添加通知活动:
<activity
android:name=".LLMNotificationActivity"
android:label="@string/app_name">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
当通知使用RemoteView
作为其内容时,我期望这个RemoteView
将启动连接到其布局定义的活动。但是似乎并没有。
以下是我在主应用程序Activity
中创建通知的方法:
protected void startNoti() {
if( noti!=null ) return;
Context context = getApplicationContext();
RemoteViews contentView = new RemoteViews(context.getPackageName(),R.layout.activity_noti1);
Notification.Builder notibuilder = new Notification.Builder(context);
notibuilder.setContentTitle(" ");
notibuilder.setContentText(" ");
notibuilder.setSmallIcon(R.drawable.ic_launcher);
notibuilder.setOngoing(true);
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
noti = notibuilder.build();
noti.contentView = contentView;
manager.notify(NOTIFICATION_ID, noti);
}
LLMNotificationActivity
活动类像往常一样被定义:
public class LLMNotificationActivity extends Activity {
.... etc.... constructor, some button on-click handlers, nothing spectacular...
}
请问有没有人能告诉我我错过了什么或者我是否误解了RemoteView
的作用?我的理解是,RemoteView
一旦创建,应该会调用与其布局相关联的活动。或者,我错过了某些API,可以明确地设置RemoteView
的意图吗?
到目前为止我所发现的只有设置内容Intent
,它基本上只是在用户触摸通知后启动一个Activity
。我要找的是处理自定义布局通知中某些UI元素的触摸,而不是无论用户在通知表面点击哪里都会启动一个Activity
。
例如,如果我在RemoteView
中有3个图标(即ImageView
),我希望能够处理每个图标上的触摸事件。我想象不出这不可能,因为如果不行的话,那么使用RemoteView
在通知中有什么意义呢?
tools
清单属性仅影响工具(例如,在基于声明的tools:context
的情况下查看布局预览时提供正确的主题),在运行时没有任何影响。 - ianhanniballake