각 완료 후에 자체를 호출하는 AsyncTask를 수행하는 서비스가 있습니다. 아래에서 볼 수 있듯이 포 그라운드에서 서비스를 시작합니다. 그것은 성공적으로 시작하고 내가 내 컴퓨터에 연결하고 LogCat에 출력을 뱉어내는 동안 의도대로 실행을 계속합니다. 나는 이것을 테스트하기 때문에 AsyncTask 루프가 매 5 분마다 알림을 뱉어 내도록했습니다. 그러나 내 컴퓨터에서 플러그를 뽑으면 알림이 표시되지 않습니다. 마치 서비스를 시작한 후에 서비스가 완전히 멈춘 것처럼 보입니다!내 Android 서비스가 계속 실행되지 않음
참고 : 내 서비스는 IntentService가 아닌 일반 서비스입니다. 여기
내 onStartCommand는
는 ... 여기@SuppressWarnings("deprecation")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
getData(intent);
self = this;
// Enter foreground state
String title = "Service started.";
String subject = "Service is running.";
String body = "Monitoring...";
Notification notification = new Notification(R.drawable.ic_launcher, title,
System.currentTimeMillis());
if(notificationSounds)
notification.defaults |= Notification.DEFAULT_SOUND;
else
notification.sound = null;
Intent notificationIntent = new Intent(this, MainActivity3.class);
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, subject, body, pendIntent);
startForeground(1500, notification);
new BatteryLifeTask(appContext).execute();
return START_NOT_STICKY;
}
내 AsyncTask를하다 :
// Looping AsyncTask for continuous mode
private class BatteryLifeTask extends AsyncTask<Void, Void, Void> {
// Member variables
Context appContext;
int batteryPct0;
public BatteryLifeTask(Context context) {
super();
appContext = context;
}
protected Void doInBackground(Void... params) {
System.out.println("Entering doInBackground");
// Get the initial battery level
batteryPct0 = getBatteryPercent();
System.out.println("Initial battery percent: " + batteryPct0);
// Check time
Calendar c = Calendar.getInstance();
Date dateNow = c.getTime();
// getTime returns ms, need minutes. 60000ms in a minute.
long currTime = dateNow.getTime()/60000;
if(currTime >= timeToUse){
finished = true;
stopSelf();
}
System.out.println("Leaving doInBackground");
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if(!finished) {
int waitTime = 60000 * interval; // 1 minute is 60000 miliseconds
System.out.println("Entering postExecute. waitTime is " + waitTime);
Runnable r = new Runnable() {
@Override
public void run() {
if(!finished) { // In case postDelayed is pending, avoids extra notification
System.out.println("An interval has passed.");
calculateHelper(batteryPct0);
new BatteryLifeTask(appContext).execute();
}
}
};
Handler h = new Handler();
h.postDelayed(r, waitTime);
}
}
}
그리고 여기에 생성 알림 내 코드입니다 :
// Method for creating a notification
@SuppressWarnings("deprecation")
void notify0(int id, String title, String subject, String body, boolean playSound){
NotificationManager NM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify = new Notification(android.R.drawable.
PendingIntent pending = PendingIntent.getActivity(
getApplicationContext(), 0, new Intent(), 0);
notify.setLatestEventInfo(getApplicationContext(), subject, body, pending);
if(playSound)
notify.defaults |= Notification.DEFAULT_SOUND;
else
notify.sound = null;
// Cancel running notification if exists
NM.cancel(id);
// Push notification
NM.notify(id, notify);
}
아무도 도와 줄 수 있어요? 이자형? 이것은 나를 미치게 만든다! 내 응용 프로그램은 플러그가 꽂혀 USB 디버깅에 연결될 때 완벽하게 작동합니다. 그러나 플러그를 뽑으면 서비스가 완전히 중단되고 아무 것도하지 않는 것처럼 보입니다.
경보 관리자 – njzk2
과 관련이 없지만 '// 1 분이 60000 밀리 초입니다'대신 http://developer.android.com/reference/android/text/format/DateUtils.html#MINUTE_IN_MILLIS – njzk2
@ 고마워, 도움이. 무엇을 위해 alarmmanager를 사용합니까? 그게 어떻게 도움이 될까요? – JayB