具有RemoteViews的Android通知-具有与RemoteViews布局相关联的活动

具有RemoteViews的Android通知-具有与RemoteViews布局相关联的活动,第1张

具有RemoteViews的Android通知-具有与RemoteViews布局相关联的活动

您必须按如下所示关联

setOnClickPendingIntent
要从远程视图启动活动的活动…您可以在中设置任何布局ID
RemoteView
单击

 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);}}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5055420.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-15

发表评论

登录后才能评论

评论列表(0条)

保存