如何在自定义通知中引用视图

在下面的发布代码中,我创build了一个自定义布局的通知。 通知的布局包含三个操作button。

我现在的问题是,我不能引用代码中的任何button,以便我可以导航到另一个基于点击的操作button的活动。我正在尝试做什么的时候是Action button 1被点击,然后Activity 1显示当点击Action button 2时, Activity 2出现,等等。

请让我知道如何引用通知的自定义布局的意见?

代码

 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Using RemoteViews to bind custom layouts into Notification RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.layout_notification); String notification_title = "Notification_Title"; String notification_text = "Notification_Text"; // Open NotificationView Class on Notification Click Intent intent = new Intent(this, NotificationReply.class); // Send data to NotificationView Class intent.putExtra("title", notification_title); intent.putExtra("text", notification_text); // Open NotificationView.java Activity PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder builder = new NotificationCompat.Builder(this) // Set Icon .setSmallIcon(R.mipmap.ic_launcher) // Set Ticker Message .setTicker("Ticker") // Dismiss Notification .setAutoCancel(true) // Set PendingIntent into Notification .setContentIntent(pIntent) // Set RemoteViews into Notification .setContent(remoteViews); Intent intentAction1 = new Intent(this, ActAction1.class); PendingIntent pendingIntentActAction1 = PendingIntent.getBroadcast(this, 1,intentAction1, PendingIntent.FLAG_UPDATE_CURRENT); remoteViews.setOnClickPendingIntent(R.id.btn_action1, pendingIntentActAction1); Intent intentAction2 = new Intent(this, ActAction2.class); PendingIntent pendingIntentActAction2 = PendingIntent.getBroadcast(this, 2,intentAction2, PendingIntent.FLAG_UPDATE_CURRENT); remoteViews.setOnClickPendingIntent(R.id.btn_action2, pendingIntentActAction2); Intent intentAction3 = new Intent(this, ActAction3.class); PendingIntent pendingIntentActAction3 = PendingIntent.getBroadcast(this, 3,intentAction3, PendingIntent.FLAG_UPDATE_CURRENT); remoteViews.setPendingIntentTemplate(R.id.btn_action3, pendingIntentActAction3); // Create Notification Manager NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); // Build Notification with Notification Manager notificationmanager.notify(0, builder.build()); } } 

Solutions Collecting From Web of "如何在自定义通知中引用视图"

你必须使用RemoteViews#setOnClickPendingIntent() API:

相当于调用setOnClickListener(android.view.View.OnClickListener)来启动提供的PendingIntent

Intent firstIntent = new Intent(context, FirstActivity.class); PendingIntent firstPendingIntent = PendingIntent.getBroadcast(this, 0, firstIntent, 0); RemoteViews notificationView = ... notificationView.setOnClickPendingIntent(R.id.first_button, firstPendingIntent);
Intent firstIntent = new Intent(context, FirstActivity.class); PendingIntent firstPendingIntent = PendingIntent.getBroadcast(this, 0, firstIntent, 0); RemoteViews notificationView = ... notificationView.setOnClickPendingIntent(R.id.first_button, firstPendingIntent); 

导航的关键成功是使用正确的挂起的意图,并为您的requestCode使用不同的值。

您可以通过broadcastReceiver接收事件,以这种方式构build挂起的意图(例如,带参数):

 private PendingIntent buildPendingIntent(String someurl) { Intent intent= new Intent(mContext, SomeReceiver.class); final Uri uri = Uri.parse(someurl); intent.setData(uri); intent.setAction(Intent.ACTION_VIEW); intent.putExtra(KEY, VALUE) return PendingIntent.getBroadcast(mContext.getApplicationContext(), CONSTANT_FOR_THIS_INTENT_TYPE, intent, PendingIntent.FLAG_CANCEL_CURRENT); } 

或者您可以通过未决意图导航到活动:

 private PendingIntent buildPendingIntent2() { Intent intent = new Intent(mContext, SomeActivity.class); intent.putExtra(KEY, VALUE); settingsIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); return PendingIntent.getActivity(mContext.getApplicationContext(), OTHER_CONSTANT_FOR_OTHER_ACTION, intent, PendingIntent.FLAG_UPDATE_CURRENT); } 

请注意, CONSTANT_FOR_THIS_INTENT_TYPEOTHER_CONSTANT_FOR_OTHER_ACTION 必须有不同的值

在你有这个意向的意图后,你应该把它们附加到你的意见:

 view.setOnClickPendingIntent(com.app.btn1, buildPendingIntent1()); view.setOnClickPendingIntent(com.app.btn2, buildPendingIntent2()); 

你必须使用RemoteViews#setOnClickPendingIntent() API:

我得到你的错误,你正尝试用PendingIntent.getBroadcast()来发起一个只发送广播消息的活动,所以理想情况下,一个广播接收器应该使用同一个intentfilter动作进行注册。 在你的情况下,它应该最好是PendingIntent.getActivity

现在,您可以启动以下悬而未决的意图标志值是不正确的,我可以为其他人承担,因此悬而未决的意图没有被解雇。

  Intent firstIntent = new Intent(context, FirstActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(appContext, 101, firstIntent, PendingIntent.FLAG_UPDATE_CURRENT); // For opening activity ideally this should work 

如果这不起作用,您可以尝试发送广播,并听取它以validation广播是否也被发送: –

 PendingIntent pendingIntent = PendingIntent.getBroadcast(appContext, 0, firstIntent, PendingIntent.FLAG_ONE_SHOT); // for sending broadcast and listens in broadcast reciever