3

由于我的另一个问题,我使用 NotificationCompat 成功创建了一个通知。我现在只想更改通知在 onClick 上的作用。我真的很想弹出一个警报对话框(我见过一些应用程序这样做),但每次我点击它时,我都会显示我的活动。有任何想法吗?

通知代码:

Intent notificationIntent = new Intent(ctx, YourClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(ctx,
        YOUR_PI_REQ_CODE, notificationIntent,
        PendingIntent.FLAG_CANCEL_CURRENT);

NotificationManager nm = (NotificationManager) ctx
        .getSystemService(Context.NOTIFICATION_SERVICE);

Resources res = ctx.getResources();
Notification.Builder builder = new Notification.Builder(ctx);

builder.setContentIntent(contentIntent)
            .setSmallIcon(R.drawable.some_img)
            .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.some_big_img))
            .setTicker(res.getString(R.string.your_ticker))
            .setWhen(System.currentTimeMillis())
            .setAutoCancel(true)
            .setContentTitle(res.getString(R.string.your_notif_title))
            .setContentText(res.getString(R.string.your_notif_text));
Notification n = builder.build();

nm.notify(YOUR_NOTIF_ID, n);
4

3 回答 3

2

没有活动就无法进行正常对话。有几种可能的解决方法,包括将 Activity 设置为像对话框一样的样式,并使 Activity 本身不可见并立即从中启动对话框。

于 2012-10-12T00:40:08.970 回答
0

您可以使用远程视图来显示一个对话框,而无需进入您的应用程序进程。

于 2012-10-12T00:45:36.627 回答
0

你可以设置

 Intent intent = new Intent(context, ReserveStatusActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

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





    intent = new Intent(String.valueOf(PushActivity.class));
    intent.putExtra("message", MESSAGE);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(PushActivity.class);
    stackBuilder.addNextIntent(intent);
    // PendingIntent pendingIntent =
    stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);


    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
            0, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.BigPictureStyle notiStyle = new
            NotificationCompat.BigPictureStyle();

    android.app.Notification notification = new NotificationCompat.Builder(context)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(en_title)
            .setContentText(en_alert)
            .setAutoCancel(true)
            .setNumber(++numMessages)
            .setStyle(notiStyle)

            //.setContentIntent(resultPendingIntent)

                   .setContentIntent(pendingIntent)

            .build();

    notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    notificationManager.notify(1000, notification); 
于 2015-09-24T20:30:02.760 回答