引导接收器调用服务时未创建通知

时间:2017-10-09 05:35:53

标签: android android-broadcastreceiver android-intentservice

我有一个在启动完成时触发的接收器

public class StartupReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction() != null && intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
            Log.i("In" , "getAction() - Boot");
            ReactivationService.enqueueWork(context, intent);
        }
        else
            Log.i("No" , "Boot");
    }
}

这会调用JobIntentService:

public class ReactivationService extends JobIntentService {

    public static final int JOB_ID = 0x01;

    public static void enqueueWork(Context context, Intent work) {
        enqueueWork(context, ReactivationService.class, JOB_ID, work);
    }

    @Override
    protected void onHandleWork(@NonNull Intent intent) {
        Log.i("In" , "onHandleWork()");
        NotificationService.setNotificationServiceAlarm(this, new Prefs(this).getNotifs());
        ...
    }
}

这是我的NotificationService类:

public class NotificationService extends IntentService {

    private static final String TAG = "NotificationsService";
    Prefs prefs;
    Notification.Builder builder;

    public NotificationService() {
        super(TAG);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return super.onBind(intent);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        CheckConnection checkNetwork = new CheckConnection(this);
        if (!checkNetwork.isNetworkAvailable()) {
            return;
        }
        Log.i("In" , "Notification onHandleIntent()");
        prefs = new Prefs(this);
        String city = prefs.getCity();
        String units = prefs.getUnits();

        try {
            WeatherInfo weather;
            Log.i("In" , "Request");
            weather = new Request(this).getItems(city, units);
            weatherNotification(weather);
        } catch (IOException e) {
            Log.e(TAG, "Error get weather", e);
        }
    }

    public static Intent newIntent(Context context) {
        Log.i("Trigger" , "newIntent");
        return new Intent(context, NotificationService.class);
    }

    public static void setNotificationServiceAlarm(Context context,
                                                   boolean isNotificationEnable) {
        if (context != null) {

            Log.i("In", "Notification Service Alarm");
            Intent intent = NotificationService.newIntent(context);
            PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            long intervalMillis = AlarmManager.INTERVAL_HOUR;
            if (alarmManager != null && pendingIntent != null)
                if (isNotificationEnable) {
                    Log.i("In", "Alarm Repeating");
                    alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                            SystemClock.elapsedRealtime(),
                            intervalMillis,
                            pendingIntent);
                } else {
                    Log.i("In", "Cancelled Pending Intent");
                    alarmManager.cancel(pendingIntent);
                    pendingIntent.cancel();
                }
        }
        else
            Log.i("Null" , "Context");
    }

    private void weatherNotification(WeatherInfo weather) {
        Intent intent = new Intent(this, WeatherActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

        Log.i("In" , "Weather Notification");
        ...

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        assert notificationManager != null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String id = "w01", name = getString(R.string.weather_notification_title);
            int importance = NotificationManager.IMPORTANCE_MIN;
            String desc = getString(R.string.weather_notification_description);

            NotificationChannel channel = new NotificationChannel(id, name, importance);
            channel.setDescription(desc);
            notificationManager.createNotificationChannel(channel);
            builder = new Notification.Builder(this , id);
        }
        else
            builder = new Notification.Builder(this);

        ...
        Notification notification = builder.build();
        notificationManager.notify(0 , notification);
    }
}

在Boot Completion中我的logcat看起来像

10-09 11:00:59.469 5534-5534/? I/zygote64: Late-enabling -Xcheck:jni
10-09 11:01:00.405 5534-5534/com.a5corp.weather I/InstantRun: starting instant run server: is main process
10-09 11:01:00.422 5534-5534/com.a5corp.weather I/In: getAction() - Boot
10-09 11:01:00.518 5534-5580/com.a5corp.weather I/In: onHandleWork()
10-09 11:01:00.989 5534-5580/com.a5corp.weather W/zygote64: Verification of void com.a5corp.weather.service.NotificationService.weatherNotification(com.a5corp.weather.model.WeatherInfo) took 247.755ms
10-09 11:01:00.990 5534-5580/com.a5corp.weather I/In: Notification Service Alarm
10-09 11:01:00.990 5534-5580/com.a5corp.weather I/Trigger: newIntent
10-09 11:01:01.004 5534-5580/com.a5corp.weather I/In: Alarm Repeating

在类似情况下,我还有一个升级接收器:

public class UpgradeReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction() != null && intent.getAction().equals(Intent.ACTION_MY_PACKAGE_REPLACED)) {
            Log.i("In" , "getAction() - Upgrade");
            ReactivationService.enqueueWork(context, intent);
        }
        else
            Log.i("No" , "Upgrade");
    }
}

这是触发时的logcat:

10-09 11:00:59.469 5534-5534/? I/zygote64: Late-enabling -Xcheck:jni
10-09 11:01:00.405 5534-5534/com.a5corp.weather I/InstantRun: starting instant run server: is main process
10-09 11:01:00.422 5534-5534/com.a5corp.weather I/In: getAction() - Boot
10-09 11:01:00.518 5534-5580/com.a5corp.weather I/In: onHandleWork()
10-09 11:01:00.989 5534-5580/com.a5corp.weather W/zygote64: Verification of void com.a5corp.weather.service.NotificationService.weatherNotification(com.a5corp.weather.model.WeatherInfo) took 247.755ms
10-09 11:01:00.990 5534-5580/com.a5corp.weather I/In: Notification Service Alarm
10-09 11:01:00.990 5534-5580/com.a5corp.weather I/Trigger: newIntent
10-09 11:01:01.004 5534-5580/com.a5corp.weather I/In: Alarm Repeating
10-09 11:03:21.927 8132-8294/com.a5corp.weather I/In: Notification onHandleIntent()
10-09 11:03:21.931 8132-8294/com.a5corp.weather I/In: Request
10-09 11:03:22.101 8132-8294/com.a5corp.weather I/In: Weather Notification

这意味着当我收到UpgradeReceiver时Job Intent服务正在工作,但StartupReceiver上没有。我不知道自己哪里出错了。帮助我。

0 个答案:

没有答案