在长时间闲置期间启动后,Android IntentService被杀死

时间:2015-08-25 15:52:01

标签: android service intentservice

我的手机闲置了几个小时后,通常大约8小时,IntentService计划将开始,但永远不会完成。我使用服务和START_REDELIVER_INTENT遇到了同样的问题。当手机正在使用或最近一直在使用时,它似乎工作正常。

未完成的代码片段是dailyReset。第一个通知消失,所以我知道它至少在终止之前就已经存在了。

public class AlarmService extends IntentService {

LocalBroadcastManager broadcaster;
static final public String REFRESH_INTENT = "rf";

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

public void onHandleIntent(Intent intent){
    broadcaster = LocalBroadcastManager.getInstance(this);
    Bundle bundle = intent.getExtras();
    String message = bundle.getString("alarm");
    if(message.equals("dosesTakenResetAlarm")){
        scheduledReminder(bundle.getInt("code"), "RESET", getApplicationContext());
        dailyReset(getApplicationContext());
        scheduledReminder(bundle.getInt("code")+1, "FINISH", getApplicationContext());
    }
    else if(message.equals("scheduledReminder")){
        scheduledReminder(bundle.getInt("code"), bundle.getString("name"), getApplicationContext());
    }
    WakefulBroadcastReceiver.completeWakefulIntent(intent);
}

private void dailyReset(Context context){
    FileWriter fw = new FileWriter(context);
    AlarmCreator ac = new AlarmCreator();
    String saveString = "";
    int start = 0;
    int medNum = 0;
    fw.fp.onloadString(fw.readFromFile());
    if(fw.fp.hasNext()){start = fw.readEntry(medNum).position1;}
    while(fw.fp.hasNext()){
        SaveData mv = fw.readEntry(medNum);
        fw.fp.onloadMedString(mv.data);
        String name = fw.fp.nextMedVariable();
        String doses = fw.fp.nextMedVariable();
        String frequency = fw.fp.nextMedVariable();
        String frequencyS = fw.fp.nextMedVariable();
        String reminders = fw.fp.nextMedVariable();
        String dosesT = fw.fp.nextMedVariable();
        frequencyS = ""+(Integer.parseInt(frequencyS)-1);
        if(Integer.parseInt(frequencyS) < 0){
            frequencyS = ""+(Integer.parseInt(frequency)-1);
        }
        //fw.replaceEntry(fw.fp.buildMedSaveString(name, doses, frequency, frequencyS, reminders, "0"), mv.position1, mv.position2);
        saveString += fw.fp.buildMedSaveString(name, doses, frequency, frequencyS, reminders, "0");
        if(frequencyS.equals("0")){
            fw.fp.onloadReminderString(reminders);
            int reminderNum = 0;
            while(fw.fp.hasNextReminder()){
                ReminderData temp = fw.fp.nextReminder();
                if(temp.half.equals("1")){
                    temp.hour = ""+(Integer.parseInt(temp.hour)+12);
                }
                ac.setAlarm(Integer.parseInt(temp.hour), Integer.parseInt(temp.minute), 0, ac.buildAlarmCode(medNum, reminderNum), name);
                reminderNum++;
            }
        }
        medNum++;
        fw.fp.nextMedication();
    }
    if(medNum > 0){
        fw.replaceEntry(saveString, start, fw.readFromFile().length());
        System.out.println(fw.readFromFile());
    }

    Intent intent = new Intent(REFRESH_INTENT);
    broadcaster.sendBroadcast(intent);
}

private void scheduledReminder(int code, String n, Context context){
    Intent intent = new Intent(context, Main.class);
    PendingIntent pIntent = PendingIntent.getActivity(context, 101, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.nicon)
                    .setContentTitle("Pillbud Reminder")
                    .setContentText("Take a dosage of "+n+".  "+code);
    mBuilder.setDefaults(Notification.DEFAULT_ALL);
    mBuilder.setContentIntent(pIntent);
    mBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
    NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    mNotifyMgr.notify(code, mBuilder.build());
}

0 个答案:

没有答案