当前位置: 首页 > 知识库问答 >
问题:

无法检索相同的挂起意图。

葛宪
2023-03-14

即使传递了相同的意图ID,我也无法检索相同的意图。

我已检查广播接收器在创建待定意图时使用的上下文是否完全相同。

public class AlarmReceiver extends BroadcastReceiver {

public static String NOTIFICATION_ID = "notification-id";
public static String NOTIFICATION = "notification";
public static MediaPlayer mMediaPlayer;
public static NotificationManager notificationManager;
public static Notification notification;
public static int id;
Context ctx;

@Override
public void onReceive(final Context context, Intent intent) {
    ctx = context;

    Log.d("WTF", ""+intent.getAction());

    if (intent.getAction() != null) {
        String action = intent.getAction();
        switch (action) {
            case "SNOOZE":
                Log.v("shuffTest", "Pressed Snoozed");
                break;
            case "STOP_ACTION":
                Log.v("shuffTest", "Pressed Stop");
                mMediaPlayer.stop();
                notificationManager.cancel(id);
                stopReceiverServices();
                break;
        }
    } else {
        notificationManager = (NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = intent.getParcelableExtra(NOTIFICATION);
        id = intent.getIntExtra(NOTIFICATION_ID, 0);
        notificationManager.notify(id, notification);
        playAlarmSound();
    }

    // This is the Intent to deliver to our service.
    //Intent service = new Intent(context, SimpleWakefulService.class);
    // Start the service, keeping the device awake while it is launching.
    //startWakefulService(context, service)
}

public void stopReceiverServices(){
    Intent notificationIntent = new Intent(ctx.getApplicationContext(), AlarmReceiver.class);
    notificationIntent.putExtra(AlarmReceiver.NOTIFICATION_ID, 123);
    notificationIntent.putExtra(AlarmReceiver.NOTIFICATION, notification);
    Log.d("TAG", "Context A1: "+ctx.getApplicationContext().toString());
    Log.d("TAG", "Context A2: "+ctx.toString());
    AlarmManager alarmManager = (AlarmManager) ctx.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    Log.d("TAG", "AlarmManager Before: " + alarmManager);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx.getApplicationContext(), 987654321, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    Log.d("TAG", "PendingIntent Before: " + pendingIntent);
    alarmManager.cancel(pendingIntent);
    pendingIntent.cancel();
    Log.d("TAG", "AlarmManager After: " + alarmManager);
    Log.d("TAG", "PendingIntent After: " + pendingIntent);
}
/..
}

创建PendingEvent时。

void setAlarm(Context context) {
    mContext = context;
    Intent notificationIntent = new Intent(context, AlarmReceiver.class);
    notificationIntent.putExtra(AlarmReceiver.NOTIFICATION_ID, 123);
    notificationIntent.putExtra(AlarmReceiver.NOTIFICATION, getNotification("Wake Up! Wake Up"));

    AlarmManager alarmManager = (AlarmManager) context.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 987654321, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
    calendar.set(Calendar.MINUTE, minuteOfHour);

    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

    Log.d("TAG", "AlarmManager After setting : " + alarmManager);
    Log.d("TAG", "PendingIntent After setting : " + pendingIntent);

    Log.d("Ctx setAlarm1 ", context.getApplicationContext().toString());
}

Logcat:

06-14 20:01:39.297 23767-23767/com.apps.testapp D/TAG: AlarmManager After setting : android.app.AlarmManager@ed1e807
06-14 20:01:39.297 23767-23767/com.apps.testapp D/TAG: PendingIntent After setting : PendingIntent{7c1d34: android.os.BinderProxy@ff6775d}
06-14 20:01:39.297 23767-23767/com.apps.testapp D/Ctx setAlarm1: com.apps.testapp.ApplicationMain@33d58d2
06-14 20:01:39.305 23767-23881/com.apps.testapp E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb91721a8
06-14 20:01:44.334 23767-23767/com.apps.testapp D/WTF: null
06-14 20:01:44.545 23767-23767/com.apps.testapp D/MediaPlayer: setSubtitleAnchor in MediaPlayer
06-14 20:01:49.772 23767-23767/com.apps.testapp D/WTF: STOP_ACTION
06-14 20:01:49.773 23767-23767/com.apps.testapp V/shuffTest: Pressed Stop
06-14 20:01:49.777 23767-23767/com.apps.testapp D/TAG: Context A1: com.apps.testapp.ApplicationMain@33d58d2
06-14 20:01:49.777 23767-23767/com.apps.testapp D/TAG: Context A2: android.app.ReceiverRestrictedContext@3af302a
06-14 20:01:49.777 23767-23767/com.apps.testapp D/TAG: AlarmManager Before: android.app.AlarmManager@ed1e807 
06-14 20:01:49.780 23767-23767/com.apps.testapp D/TAG: PendingIntent Before: PendingIntent{cc07e1b: android.os.BinderProxy@8441bb8}
06-14 20:01:49.782 23767-23767/com.apps.testapp D/TAG: AlarmManager After: android.app.AlarmManager@ed1e807
06-14 20:01:49.782 23767-23767/com.apps.testapp D/TAG: PendingIntent After: PendingIntent{cc07e1b: android.os.BinderProxy@8441bb8}

共有1个答案

龚凯泽
2023-03-14

在alarmManager和PendingEvent中删除GetApplicationNext()

void setAlarm(Context context) {

...

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context), 987654321, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

...
}
 类似资料:
  • 我正在用Android开发一个报警应用程序。流程非常简单,我只是创建了一个pendingent,然后在AlarmManager中调用setExact()方法,如下所示。 经过一些测试,我意识到,使用上面的代码片段,我只能设置一个警报,因为我将PendingContent的requestCode设置为零,如果我将另一个警报的requestCode设置为0,那么它将覆盖前一个警报。有没有办法在不更改请

  • 这是设置u和删除通知的代码。如果你需要更多的细节,请告诉我。堆栈溢出的唯一解决方案是关于相同的挂起意图。我已经尝试过这个解决方案,但没有成功。

  • 我正在创建一个ApacheFTPS客户端(因为远程服务器不允许普通FTP)。我可以毫无问题地连接和删除文件,但在使用retrieveFile()或retrieveFileStream()时,它会挂起。 出于某种原因,非常小的文件确实会传输(高达5792字节),但其他任何东西都会给出以下PrintExpldListener输出: 运行: 220------欢迎使用纯FTPd[privsep][TLS

  • 我在通知和等待意向方面遇到了很大困难。我正在尝试打开聊天活动,其中包含发送消息的相应用户详细信息。这就是为什么我在Firebase函数中传递了发送消息的用户id。我在FCM中获得了正确的日志,但当我收到聊天通知并打开它时,它会打开活动,而没有任何用户名和消息。它将使用默认值打开活动的新实例。 消息负载: 我的清单如下: 我不知道我是否添加了一些不同的功能,比如LifeCycleEvent List

  • 不确定是否有人遇到过这个问题。我正在使用带有EJB3.1的JBoss7.1应用程序服务器和带有HiberNate实现的JPA2.0。我正在使用后端Oracle 11g数据库。请参阅下面的代码。我正在使用由HTTP GET请求驱动的servlet调用我的EJB。 当我在下面的代码中注释掉 时,我的网页会永远挂起,一段时间后我会得到下面的堆栈跟踪(如果我调试,它会让我直到 persist() 方法,然