使用PendingIntent来实现我的方法

时间:2015-10-09 13:11:38

标签: java android android-studio notifications

我有一个提供通知的Android应用程序。我可以与按钮一起生成通知,没有问题。我的问题是按钮的动作。我希望按钮调用方法,例如文本打印方法。我正在使用addAction(图标,"标题",pendingIntent)。

public void sendNotification(Context context) {

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent intent = new Intent(context.getApplicationContext(), SomeClass.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context.getApplicationContext(), (int) System.currentTimeMillis(), intent, 0);

    NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.icon)
            // This creates the button and it is using a pending intent.
            .addAction(R.drawable.icon, "Print something", printPendingIntent)
            // Clicking on notification takes you back to the App.
            .setContentIntent(pendingIntent)
            .setAutoCancel(true)
            .setContentTitle("My Title")
            .setContentText("Some text");

    notificationManager.notify(NOTIFICATION_ID, builder.build());
}

所以问题在于printPendingIntent,(没有编写代码,你可以看到), 如何使用此挂起的意图访问/调用方法?

感谢您的回答,如果问题不够明确和详细,请对不起。

1 个答案:

答案 0 :(得分:0)

添加intent.putExtra(“key”,“printmethod”);然后使用getIntent()接收SomeClass中的数据.getExtras(“Key”);如果值匹配,那么您可以在该活动中调用所需的方法。