在活动中将活动作为参数传递

时间:2016-04-01 01:09:55

标签: android android-notifications android-pendingintent

我将通知定义为:

   private void sendNotification(String msg, Activity onClickActivity) {

        mNotificationManager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);

        // Send to the login activity, since if already a member and logged in, should be able to start that activity

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, onClickActivity), 0);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.gcm_cloud)
                .setContentTitle("Hello")
                .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                .setContentText(msg);

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

    }

如您所见,我希望能够允许动态引用PendingIntent,以便用户可以单击通知并随后访问给定Activity onClickActivity的不同活动。目前错误指出它无法解析构造函数。

我怎样才能让它发挥作用?

1 个答案:

答案 0 :(得分:1)

Intent constructor需要class,而不是instance。只需将方法签名更改为

即可
private void sendNotification(String msg, Class<? extends Activity> onClickActivity) {