백그라운드에서 20 초마다 실행되는 Android 서비스를 실행하고 사용자의 lat-long 데이터를 추적. 신청서를 처음 시작할 때 작동합니다. 지금 홈 버튼을 클릭하면 배경에서 계속 실행됩니다. 하지만 이제 홈 버튼을 사용하여 앱 목록에서 내 애플리케이션을 삭제하면됩니다. 런처 아이콘으로 내 앱을 다시 시작하십시오. 이제 서비스가 시작되지 않습니다. 나는 Alarm Manager를 사용하여 20 초마다 서비스를 시작합니다. 그러나 재시작시에는 내 경보가 설정되지만 결과 내 서비스가 호출되지 않으므로 브로드 캐스트 수신기에 등록되지 않습니다. MyFragment.java의 onCreateView() 내가 내 알람을 설정하고있다 : - - :AlarmManager를 사용하여 매 20 초마다 Android 서비스 실행이 앱 목록에서 앱을 종료 할 때 다시 시작되지 않음
Intent alarm = new Intent(mContext, AlarmReceiver.class);
boolean alarmRunning = (PendingIntent.getBroadcast(mContext, 0, alarm, PendingIntent.FLAG_NO_CREATE) != null);
if (alarmRunning == false) {
Log.e("In OnCreateView DDFrag", "AlarmRunning == False");
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, alarm, 0);
AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 20000, pendingIntent);
} else{
Log.e("In OnCreateView DDFrag", "AlarmRunning == True");
}
AlarmReceiver.class : -
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent background = new Intent(context, MyService.class);
Log.e("AlarmReceiver", "Broadcasr Receiver started");
context.startService(background);
}
}
MyService.class 다음은 내 코드입니다 -
public class MyService extends Service {
public boolean isServiceRunning;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
this.isServiceRunning = false;
}
@Override
public void onDestroy() {
this.isServiceRunning = false;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(!this.isServiceRunning) {
sendDataToServer();
this.isServiceRunning = true;
}
return START_STICKY;
}
private void sendDataToServer() {
// Performing my operation in this method..
// On Success of the method performed I am calling the below method and setting the below variables:
stopSelf();
this.isServiceRunning = false;
}
}
또한 나는 같은 manifest.xml 파일 내 서비스와 수신기를 정의하고있다 : -
<service android:name="com.mypackagename.services.MyService" />
<receiver android:name="com.mypackagename.services.AlarmReceiver" />
내가 문제를 해결하는 데 도움이, 또는 내가 wroung 뭐하는 거지 날 지점하시기 바랍니다. 처음으로. 내 알람 관리자가 설정되지 않았으므로 설정되고 서비스는 20 초 후에 호출됩니다. 그러나 응용 프로그램을 종료하고 다시 시작하면 내 경보가 설정되어 다시 시작되거나 다시 설정되지 않습니다. 이제 내 AlarmReceiver 클래스는 Alarm BroadcastReceiver를 수신하지 않습니다.
당신이 알람을 설정하려면 "alarmRunning"코드를 제거하십시오
어떤 기기를 테스트하고 있습니까? –
@DavidWasser Lenovo A6010 Slot –
에서 테스트 중입니다. "보호 된 앱"목록이나 백그라운드에서 실행할 수있는 앱 목록에 앱을 추가 했습니까? Lenovo에는 "설정 -> 전원 -> 백그라운드 앱 관리"와 같은 것이 있어야합니다. 앱을 찾아 목록에 추가하십시오. 네가 찾은 것을 내가 알게 해줘. –