您必须按如下所示关联
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);}}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)