在 IntentService class 中收到一个意图

Received an intent in the IntentService class

我有一个复选框列表,用户可以在其中选择一些项目,然后我将他的选择包含在 json 格式中,随后我从 alarmManager 中触发 json 字符串,然后尝试将其发送到 GetLLRD IntentService class 但我面临问题 我在 GetLLRD class 中没有收到意图,因为我在 GetLLRD class.

中没有收到任何输出

我该如何解决?

MainActivity 中的代码:

                Intent intentJson = new Intent(MainActivity.this, GetLLRD.class);
                intentJson.putExtra("json_data", json);

                PendingIntent pintent = PendingIntent.getService(getApplicationContext(), 3, intentJson, 0);
                AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                Calendar cal = Calendar.getInstance();
                alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                        5 * 1000, pintent);

                System.out.println("test intentJson output: " +intentJson);
                startService(intentJson);

GetLLRD class:

public class GetLLRD extends IntentService {

    public GetLLRD(String name) {
        super(name);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String jSONString = intent.getStringExtra("json_data");
        System.out.println("test json is " + jSONString);
        return super.onStartCommand(intent, flags, startId);
    }


    @Override
    protected void onHandleIntent(Intent intent) {

        String jSONString = intent.getStringExtra("json_data");
        System.out.println("Test" + jSONString);
        if(jSONString != null){

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

    }
}

You have to register broadcast receiver in pending intent.you may doing wrong by creating pending intent with service.for get notified from alarm manager we need to use broadcast receiver.

注册AlarmManager

        int REQUEST_CODE= 0;
        Calendar calendar = Calendar.getInstance();
        int hour = 13;
        int minute = 00;
        calendar.set(Calendar.HOUR_OF_DAY, hour);//24 Hour format
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, 00);
        /*calendar.set(Calendar.AM_PM, peried.equalsIgnoreCase("AM") ? Calendar.AM : Calendar.PM);*/
        Intent myIntent = new Intent(context, MyReceiver.class);
        myIntent.putExtra("json_data", json);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                    5 * 1000, pendingIntent );

接收器

public class MyReceiver extends BroadcastReceiver {


    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            String action = intent.getStringExtra("json_data");
            new ShowToast(context, action);
            if (action.length() > 1) {

                    startService(context, action);

            }
        } catch (Exception e) {
        }

    }

    public void startService(Context context, String action) {
        Intent service1 = new Intent(context, GetLLRD.class);
        service1.putExtra("json_data", action);
        context.startService(service1);
    }

}

在清单中注册接收器并请求使用 AlarmManager 权限。

<uses-permission android:name="android.permission.WAKE_LOCK" />

<receiver android:name=".MyReceiver" />