我构建了包含按钮的自定义通知,我想在用户按下时列出。
按钮不应该打开任何活动,只有逻辑人员才能更改歌曲。
守则:
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification);
contentView.setTextViewText(R.id.toptext, nowPlayingTitle);
//this not work
Intent intent = new Intent(this, receiver.class);
intent.putExtra("UNIQ", "1");
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, PendingIntent.FLAG_CANCEL_CURRENT)
contentView.setOnClickPendingIntent(R.id.imageButtonPlay,
pendingIntent);
notification.contentView = contentView;
// this is to return to my activity if click somwhere else in notification
Intent notificationIntent = new Intent(this, MYACTIVITY.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
mNotificationManager.notify(NOTIFICATION_ID, notification);
我没有得到setOnClickPendingIntent的内容需要在第二个参数中? 如何在用户按下按钮后调用功能?
我可能会遗漏一些因为我不理解接收方和用户按下后发生的事情
答案 0 :(得分:3)
您错过了您创建的按钮实际上不属于您的应用程序的事实。它是在另一个上下文中创建的,在另一个进程中。它无法调用你的功能。
相反,当用户点击按钮时,会触发该挂起的意图。您可以通过接收器(在您的活动中)捕获它,检查一些参数并执行操作。 或者您可以实现服务并在后台处理此意图。我更喜欢这种方式。
答案 1 :(得分:0)
感谢您的快速回答。我尝试使用接收器但它从未被解雇。 代码在主要问题中,我为接收器类创建了以下代码:
public class receiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
}
}
但是点击通知永远不会触发接收器(在调试模式下测试)