Java 带有 RemoteViews 的 Android 通知 - 具有与 RemoteViews 布局相关联的活动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22585696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android notification with RemoteViews - having activity associated with RemoteViews layout
提问by Sinisa
I've been researching on how to create custom-layout notification using RemoteView
.
我一直在研究如何使用RemoteView
.
So far, I am able to create a notification with contentView
and bigContentView
pointing to a RemoteView
with a custom layout xml. However, what does not happen, is to have Activity
(associated with custom layout) started when this RemoteView
is created.
到目前为止,我能够创建一个带有自定义布局 xml的通知contentView
并bigContentView
指向一个RemoteView
。但是,没有发生的是在创建Activity
时启动(与自定义布局相关联)RemoteView
。
I've double checked and in my layout xml, it appears to have correct Activity
class name:
我已经仔细检查过,在我的布局 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>
In manifest file, right after main application main activity, notification activity is also added:
在清单文件中,在主应用程序主活动之后,还添加了通知活动:
<activity
android:name=".LLMNotificationActivity"
android:label="@string/app_name">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I would expect when notification uses RemoteView
for its content, that this RemoteView
will launch activity that is attached to its layout definition. However it appears not.
我希望当通知RemoteView
用于其内容时,这RemoteView
将启动附加到其布局定义的活动。然而它似乎不是。
Here is how I create a notification in main application Activity
:
这是我在主应用程序中创建通知的方法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
activity class is defined as usual:
LLMNotificationActivity
活动类像往常一样定义:
public class LLMNotificationActivity extends Activity {
.... etc.... constructor, some button on-click handlers, nothing spectacular...
}
Can anyone point to me what I am missing or if I have misunderstood what RemoteView
can do? My understanding is that RemoteView
should, once created, invoke activity associated with its layout. Or - is there some API I've missed that explicitly can set intent of the RemoteView
?
任何人都可以指出我缺少什么,或者如果我误解了RemoteView
可以做什么?我的理解是RemoteView
,一旦创建,就应该调用与其布局相关的活动。或者 - 是否有一些我遗漏的 API 可以明确设置 的意图RemoteView
?
What I have found so far are only setting content Intent
which basically just launches an Activity
once user touches notification. What I'm looking for is to handle touches to some of UI elements inside custom-layout notification, not to launch an Activity
regardless where the user clicks on notification surface.
到目前为止我发现的只是设置内容Intent
,这些内容基本上只是启动Activity
一次用户触摸通知。我正在寻找的是处理对自定义布局通知中的某些 UI 元素的触摸,而不是Activity
无论用户单击通知表面的何处都启动。
For example, if I have 3 icons (i.e. ImageView
) in a RemoteView
which notification uses, I'd like to be able to handle touch on each one of them. I can't imagine this wouldn't be possible as if it's not, what's the point of having RemoteView
in notification?
例如,如果我ImageView
在RemoteView
通知中使用了3 个图标(即),我希望能够处理每个图标上的触摸。我无法想象这是不可能的,好像不是,RemoteView
通知有什么意义?
采纳答案by Libin
You have to associate the activity thought setOnClickPendingIntent
to launch the activity from remote view as below...You can set any of the layout id in the RemoteView
to click.
您必须将被认为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);
provide an +id/layout_id
to the RelativeLayout
your using.
提供+id/layout_id
给RelativeLayout
您的使用。
If you have to launch the activity when user click on the notification, then you have to use PendingIntent
as....
如果您必须在用户单击通知时启动活动,那么您必须使用PendingIntent
as....
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());
For 3 buttons , You have to use create a custom RemoteView
and use PendingIntent
. some thing as below...
对于 3 个按钮,您必须使用创建自定义RemoteView
并使用PendingIntent
. 一些事情如下...
Here is the custom remote view i am using for one of my media player app. it has three button to handler click.
这是我用于我的媒体播放器应用程序之一的自定义远程视图。它有三个按钮来处理点击。
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);
}
}
回答by IgorGanapolsky
All you need to do is addsetContentIntent
to your Notification.Builderlike this:
您需要做的就是像这样添加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);
Now, when you click on your notification, YourLanchedActivitywill be launched.
现在,当您点击通知时,YourLanchedActivity将被启动。