Android 通知操作:未决 Intent 不起作用

Android Notification Action: pending Intent not working

我在 Android 通知操作方面遇到问题。 这是我的想法: 我的 BroadcastReceiver 在广播中醒来,然后创建通知。 此通知应:

我设法让 ACTION_SEND 在点击通知时正常工作,但我没有运气 action...

在网络上找不到任何东西,只有 pendingIntents 打开活动。

这是我的代码:

public class AlarmReceiver extends BroadcastReceiver {
static int notificationId = 0;
@Override
public void onReceive(Context context, Intent intent) {
    Log.d("AlarmReceiver","ricevuto " + intent.getAction());
    Calendar calendar = Calendar.getInstance();
    String tipo = "sconosciuto";
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
    mBuilder.setSmallIcon(R.drawable.ic_flag_black_24dp);
    if(intent.getAction().equals(Constants.ALARM_ACTION_PALINDROM)){
        tipo="palindromo";
    }
    if(intent.getAction().equals(Constants.ALARM_ACTION_DOUBLE)){
        tipo="simmetrico";
    }

    mBuilder.setContentTitle("Orario " + tipo + "!");

    String currentTime = new SimpleDateFormat("HH:mm").format(new Date());
    mBuilder.setContentText(currentTime);

    Intent shareIntent = new Intent();

    Intent notifIntent = new Intent(context,MainActivity.class);
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(Intent.EXTRA_TEXT,"Sono le " + currentTime +", " + tipo + "\u2764");
    Intent shareIntentWithChooser = Intent.createChooser(shareIntent,"Send to");

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(notifIntent);

    PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent pendingIntentShare = PendingIntent.getBroadcast(context,notificationId,shareIntentWithChooser,PendingIntent.FLAG_UPDATE_CURRENT);

    mBuilder.addAction(R.drawable.ic_send_black_18dp,"Condividi",pendingIntentShare);

    mBuilder.setContentIntent(pendingIntent);
    Notification notification = mBuilder.build();
    notification.flags = Notification.DEFAULT_LIGHTS|Notification.FLAG_AUTO_CANCEL;

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(notificationId,notification);
    notificationId++;
}

}

提前致谢,我实在想不通!

你应该使用 getActivity() 而不是 getBroadcast() 来创建你的 PendingIntent,因为你想用 ACTION_SHARE 开始一个 Activity 而不是发送一个广播。