繁体   English   中英

AlarmManager无法启动服务

[英]AlarmManager not Starting Service

以前,警报管理器正在工作。 我认为我没有改变任何东西,但是现在根本没有开始。

这是我设置警报管理器的代码:

SettingsActivity.java

Intent intent;
static PendingIntent recurringDownload;

intent  = new Intent(context, UpdateScoresService.class);

recurringDownload = PendingIntent.getService(context, 0, intent, 0);
 Preference.OnPreferenceChangeListener refreshListener = new Preference.OnPreferenceChangeListener() {
                @Override
                public boolean onPreferenceChange(Preference preference, Object newValue) {
                    if(newValue.toString().equals("1")){ /* daily */
                        background_refresh.setSummary("Scores will be refreshed daily.");
                        AlarmManager manager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
                        manager.cancel(recurringDownload);
                        recurringDownload.cancel();
                        Log.e("DAILY REFRESH", " ");
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTimeInMillis(System.currentTimeMillis());
                        calendar.set(Calendar.HOUR_OF_DAY,10);
                        calendar.set(Calendar.MINUTE,00);
                        if(calendar.before(Calendar.getInstance())){
                            Log.e("AFTER", "10 AM DAILY");
                            calendar.add(Calendar.DATE, 1);
                        }
                        manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, recurringDownload);
                    }else if(newValue.toString().equals("2")){ /* weekly */
                        Log.e("WEEKLY REFRESH", " ");
                        background_refresh.setSummary("Scores will be refreshed weekly.");
                        AlarmManager manager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
                        manager.cancel(recurringDownload);
                        recurringDownload.cancel();
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTimeInMillis(System.currentTimeMillis());
                        calendar.set(Calendar.HOUR_OF_DAY,10);
                        calendar.set(Calendar.MINUTE,00);
                        if(calendar.before(Calendar.getInstance())){
                            Log.e("AFTER", "10 AM WEEKLY");
                            calendar.add(Calendar.DATE, 1);
                        }
                        manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, recurringDownload);
                    }else{ /* manually */
                        background_refresh.setSummary("Scores will be refreshed manually.");
                        Log.e("MANUAL REFRESH", " ");
                        AlarmManager manager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
                        manager.cancel(recurringDownload);
                        recurringDownload.cancel();

                    }
                    return true;
                }
};

UpdateScoresService在这里:

public class UpdateScoresService extends IntentService {

    public int countChanged;
    Context context = this;

    public UpdateScoresService() {
        super("UpdateScoresService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.e("onHandleIntent", "grabbing scores");
        countChanged = new GetAnimeScores(getApplicationContext()).refreshScores();

        if(countChanged>0){ //Display notification if any scores changed
            Log.d("Creating notification", " ");
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
            builder.setSmallIcon(R.drawable.ic_timeline_white_24dp);
            builder.setContentTitle("MAL Score Tracker");
            builder.setAutoCancel(true);

            if(countChanged==1){
                builder.setContentText("1 score changed since you were gone!");
            }else{
                builder.setContentText(countChanged+" scores changed since you were gone!");
            }

            Intent intent1 = new Intent(context, MainActivity.class);
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
            stackBuilder.addParentStack(MainActivity.class);
            stackBuilder.addNextIntent(intent1);
            PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
            builder.setContentIntent(pendingIntent);

            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(0,builder.build());
        }
    }
}
}

打印“设置活动”中的“日志”,但不打印“服务中的onHandleIntent”中的“日志”。 我不知道怎么了。

最好有一个BroadcastReceiver负责启动服务。 它的代码应如下所示:

创建一个BroadcastReceiver类:

public class ReceiverToStartService extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {

        Intent i = new Intent(context, UpdateScoresService.class);
        ComponentName service = context.startService(i);
    }
}

在清单中注册接收者:

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

现在,将要传递给PendingIntent的Activity中的Intent更改为:

intent  = new Intent(context, ReceiverToStartService.class);
recurringDownload = PendingIntent.getService(context, 0, intent, 0);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM