2017-05-03 1 views
0

나는 그래서 이것은 어쩌면 정말 기본적인 질문을 안드로이드 매우 새로운 오전 매 N 초마다 반복하십시오. 초는 Android 앱의 설정 탭에서 설정됩니다. 예를 들어,업데이트/다시 시작 안드로이드 서비스 환경

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 

을하고 설정을 설정합니다 : 서비스의 ONSTART() 메소드에서

은 환경 설정을 얻을

final Long pollInterval = Long.parseLong(settings.getString("pollInterval", "1000")); 

작업이 새로운 져야 할 엔트리에 대한 데이터베이스에 폴링입니다. 내 목표는 메인 프레임으로 돌아간 후 서비스를 다시 시작하여 onCreate 메소드가 새 기본 설정을 가져 오거나 실행중인 태스크의 기본 설정을 새로 고침하여 폴 간격을 변경하는 것입니다.

내가 가진 서비스를 다시 시작하려고 :

startService(serviceIntent); 
stopService(serviceIntent); 

을하지만 어떤 이유로이 작동하지 않습니다.

미리 답변 해 주셔서 감사합니다.

답변

0
Use this one to repeat service 



try { 

      //Create a new PendingIntent and add it to the AlarmManager 
      Intent intent = new Intent(this, Service.class); 
      PendingIntent pendingIntent = PendingIntent.getService(this, 
       12345, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
      AlarmManager am = 
       (AlarmManager)getSystemService(Activity.ALARM_SERVICE); 
      am.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), 
        2*60*60,pendingIntent); 

      } catch (Exception e) {} 

Also stop service when your task is completed 
관련 문제