【问题标题】:What happens if I send the same pending intent twice?如果我两次发送相同的待处理意图会发生什么?
【发布时间】:2017-04-28 05:02:52
【问题描述】:

我正在尝试发送两个待处理的意图,假设我在 1 分钟后发送了我的第一个意图,在 2 分钟后发送了我的第二个意图。我对它们都使用相同的操作。 所发生的只是第二个意图有效,但也是 2 分钟后。

 AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent intent;
        intent = new Intent(ALARM_ACTION_NAME);
        if(val1) {

            intent.putExtra("TYPE","SILENT");
        }
        else {

            intent.putExtra("TYPE", "RING" );

        }


        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 2,intent, 0);

广播接收器的位置如下...

if(ALARM_ACTION_NAME.equals(intent.getAction())){
            Log.i("this", intent.getStringExtra("TYPE"));
            if(intent.getStringExtra("TYPE").equals("SILENT")) {
                Toast.makeText(context, "illumy set the phone to Silent", Toast.LENGTH_LONG).show();
                AudioManager am = (AudioManager) context.getSystemService(context.AUDIO_SERVICE);
                am.setStreamVolume(AudioManager.STREAM_RING, 0, 0);
                am.setStreamVolume(AudioManager.STREAM_MUSIC, 0 , 0);

            }
            else if (intent.getStringExtra("TYPE").equals("RING")){

                Toast.makeText(context, "Phone set the phone to Ring", Toast.LENGTH_LONG).show();
                AudioManager am = (AudioManager) context.getSystemService(context.AUDIO_SERVICE);
                am.setStreamVolume(AudioManager.STREAM_RING, 100, 0);
                am.setStreamVolume(AudioManager.STREAM_MUSIC, 30 , 0);
            }
        }

【问题讨论】:

  • 最后一个意图将始终替换为较早。

标签: java android android-pendingintent


【解决方案1】:

最后的意图总是会被更早的取代。因此有两种解决方案,一种是您定义多个操作,另一种(最好的)管理数据库中的所有调用。这意味着在执行后首先安排最低的即将到来的调用,删除该调用并再次获得即将到来的最低调用。

【讨论】:

    猜你喜欢
    • 2010-12-02
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    • 2021-09-06
    • 2017-05-20
    相关资源
    最近更新 更多