我一直在研究如何使用RemoteView
创建自定义布局通知。
到目前为止,我可以创建一个通知,contentView
和bigContentView
指向带有自定义布局xml的RemoteView
。但是,未发生的情况是,在创建Activity
时启动RemoteView
(与自定义布局关联)。
我已仔细检查过,在我的布局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
。
例如,如果我在通知使用的ImageView
中有3个图标(即RemoteView
),我希望能够处理每个图标上的触摸。我无法想象这是不可能的,好像它不是,通知RemoteView
有什么意义?
答案 0 :(得分:8)
您必须将活动思想setOnClickPendingIntent
与远程视图中的活动相关联,如下所示...您可以在RemoteView
中设置任意布局ID以进行点击。
Intent intent = new Intent(context, YourActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews removeWidget = new RemoteViews(context.getPackageName(), R.layout.your_layout);
removeWidget.setOnClickPendingIntent(R.id.layout_id, pendingIntent);
为您使用的+id/layout_id
提供RelativeLayout
。
如果您必须在用户点击通知时启动活动,则必须使用PendingIntent
作为....
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("title")
.setContent(mRemoteControl);
Intent notificationIntent = new Intent(this, YourActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(
this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(contentIntent);
mNM.notify(1000,mBuilder.build());
对于3个按钮,您必须使用创建自定义RemoteView
并使用PendingIntent
。有些事情如下......
以下是我用于其中一个媒体播放器应用的自定义远程视图。它有三个按钮来处理点击。
public class RemoveControlWidget extends RemoteViews
{
private final Context mContext;
public static final String ACTION_PLAY = "com.mediabook.app.ACTION_PLAY";
public static final String ACTION_PREVIOUS = "com.mediabook.app.ACTION_PREVIOUS";
public static final String ACTION_NEXT = "com.mediabook.app.ACTION_NEXT";
public RemoveControlWidget(Context context , String packageName, int layoutId)
{
super(packageName, layoutId);
mContext = context;
Intent intent = new Intent(ACTION_PLAY);
PendingIntent pendingIntent = PendingIntent.getService(mContext.getApplicationContext(),100,
intent,PendingIntent.FLAG_UPDATE_CURRENT);
setOnClickPendingIntent(R.id.play_control,pendingIntent);
setOnClickPendingIntent(R.id.pause_control,pendingIntent);
intent = new Intent(ACTION_PREVIOUS);
pendingIntent = PendingIntent.getService(mContext.getApplicationContext(),101,
intent,PendingIntent.FLAG_UPDATE_CURRENT);
setOnClickPendingIntent(R.id.previous_control,pendingIntent);
intent = new Intent(ACTION_NEXT);
pendingIntent = PendingIntent.getService(mContext.getApplicationContext(),102,
intent,PendingIntent.FLAG_UPDATE_CURRENT);
setOnClickPendingIntent(R.id.next_control,pendingIntent);
}
}
答案 1 :(得分:1)
您需要做的就是将setContentIntent
添加到 Notification.Builder ,如下所示:
Intent i = new Intent(context, YourLaunchedActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, 0);
Notification.Builder notibuilder = new Notification.Builder(context);
notibuilder.setContentIntent(pendingIntent);
现在,当您点击通知时, YourLanchedActivity 将会启动。
答案 2 :(得分:0)
*您可以通过执行以下操作来实现点击远程视图的待定意图 还要注意的是 如果您尝试从远程视图获取活动,则在未决意图中使用 getAcitvity 像 *
//i will create a simple method//
//use return getActivity cause getting the bottom Activity
private static PendingIntent getActivityBottom(Context context) {
clearNotification(context);
Intent intentStartActivity = new Intent(context, BotomNavViewActivity.class);
intentStartActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//intentStartActivity.addFlags(/*Intent.FLAG_ACTIVITY_CLEAR_TASK |*/ Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
return PendingIntent.getActivity(
context,
2,
intentStartActivity,
PendingIntent.FLAG_UPDATE_CURRENT);
}
public static void clearNotification(Context context) {
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);
}
现在,如果您想获得您的服务类,请在挂起的意图中使用 getService
private static PendingIntent stopPlayer(Context context){
Intent serviceIntent = new Intent(context, MusicService.class);
serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.stopService(serviceIntent);
mContentView.setImageViewResource(R.id.notif_play, R.drawable.ic_play);
return PendingIntent.getService(
context,
2,
serviceIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
}
像这样使用上面的方法
//perfoam action on notification to go to activity
mContentView.setOnClickPendingIntent(R.id.notif_small,getActivityBottom(context));
//for service
mContentView.setOnClickPendingIntent(R.id.notif_play,stopPlayer(context));