1

我构建了包含按钮的自定义通知,并且我想在用户按下它时列出。

该按钮不应打开任何活动,而只能打开诸如更改歌曲之类的逻辑人员。

编码:

        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 的第二个参数需要什么?用户按下按钮后如何调用函数?

我可能错过了一些东西,因为我不了解接收方以及用户按下后发生了什么

4

2 回答 2

3

您错过了您创建的按钮实际上不属于您的应用程序的事实。它是在另一个上下文中,在另一个过程中创建的。它无法调用您的函数。

相反,当用户点击按钮时,会触发待处理的意图。您可以通过接收器(在您的活动中)捕获它,检查一些参数并执行操作。或者你可以实现一个服务并在后台处理这个意图。我更喜欢这种方式。

于 2013-02-05T16:09:54.077 回答
0

感谢您的快速回答。我尝试使用接收器,但它从未触发。代码在主要问题中,我为接收者类创建了以下代码:

public class receiver extends BroadcastReceiver 
{   
    @Override
    public void onReceive(Context context, Intent intent) 
    {
   Bundle bundle = intent.getExtras(); 
    }      
}

但单击通知永远不会触发接收器(在调试模式下测试)

于 2013-02-05T16:15:11.577 回答