onHandleIntent receiving the fired intent every second instead of every 30 seconds

淺唱寂寞╮ 提交于 2019-12-11 14:35:55

问题


I have checkboxlist where the user selectes some items I am envloping his selecting in json format thenI am firing the json string from the alarmManager to the GetLLRD class. Currently I am getting intent in the OnHandleIntent not every 30 second but roughly every second or several times in one second. How can I manage it to get the intent just once every 30 seconds in the onHandleIntent in the GetLLRD class?

Some of the output:

07-07 17:18:01.817: I/System.out(6493): test from the onHandleIntent{"selected":[6,9]}
07-07 17:18:02.828: I/System.out(6493): test from the onHandleIntent{"selected":[6,9]}
07-07 17:18:04.049: I/System.out(6493): test from the onHandleIntent{"selected":[6,9]}
07-07 17:18:04.810: I/System.out(6493): test from the onHandleIntent{"selected":[6,9]}
07-07 17:18:05.821: I/System.out(6493): test from the onHandleIntent{"selected":[6,9]}
07-07 17:18:06.822: I/System.out(6493): test from the onHandleIntent{"selected":[6,9]}
07-07 17:18:07.822: I/System.out(6493): test from the onHandleIntent{"selected":[6,9]}
07-07 17:18:08.843: I/System.out(6493): test from the onHandleIntent{"selected":[6,9]}
07-07 17:18:09.824: I/System.out(6493): test from the onHandleIntent{"selected":[6,9]}
07-07 17:18:10.835: I/System.out(6493): test from the onHandleIntent{"selected":[6,9]}
07-07 17:18:11.876: I/System.out(6493): test from the onHandleIntent{"selected":[6,9]}
07-07 17:18:12.907: I/System.out(6493): test from the onHandleIntent{"selected":[6,9]}

MainActivity class:

                Intent intent = new Intent(MainActivity.this,
                        GetLLRD.class);
                intent.putExtra("json_data", json);
                PendingIntent pendingIntent = PendingIntent
                        .getBroadcast(getApplicationContext(), 3,
                                intent,
                                PendingIntent.FLAG_UPDATE_CURRENT);
                AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                Calendar cal = Calendar.getInstance();
                alarm.setRepeating(AlarmManager.RTC_WAKEUP,
                        cal.getTimeInMillis(), 30 * 1000, pendingIntent);
                startService(intent);

GetLLRD class:

public class GetLLRD extends IntentService {

    public GetLLRD() {
        super("IntentService");

    }

    @Override
    protected void onHandleIntent(Intent intent) {

        String jSONString = intent.getStringExtra("json_data");
        System.out.println("test from the onHandleIntent" + jSONString);
        if(jSONString != null){

            System.out.println("Test");
        }

    }
}

When I try to receive the intent in the IntentReceiver then I am getting it every 30 seconds but in the onHandleIntent I am receiving it too quick as I mentioned earlier.

IntentReceiver:

public class IntentReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            String action = intent.getStringExtra("json_data");

            if (!action.isEmpty()) {
                System.out.println("test from IntentReiceier" + action);

            }
        } catch (Exception e) {
        }

    }


}

来源:https://stackoverflow.com/questions/31272730/onhandleintent-receiving-the-fired-intent-every-second-instead-of-every-30-secon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!