2017-01-04 3 views
1

この質問は何度も尋ねられますが、私のアプリが殺されてもサービスを存続させる解決策は見つかりませんでした。 私のアプリケーションのすべてのデバイス上で実行が、一部のデバイス私はアプリを殺すならば、私のサービスも(デバイス名MI 4バージョンのANSのASUS 5.0.3を)殺したMIフォアグラウンドサービスがMI 4デバイス(バージョン5.0.2)で殺される

次は私が私を開始している私のサービスコードでありますフォアグラウンドサービス

@Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
Notification notification = new NotificationCompat.Builder(this) 
        .setContentTitle("My service started at ") 
        .setTicker("My service started") 
        .setContentText("app") 
        .setSmallIcon(R.drawable.app) 
        .setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false)) 
        .setContentIntent(pendingIntent) 
        .setOngoing(true).build(); 
      startForeground(Constants.FOREGROUND_SERVICE,notification); 
} 
+0

を、それを起動し、次に実行されていない場合、私のサービスは現在、稼働しているかどうかを確認するServiceDetector.java

import android.app.ActivityManager; import android.content.Context; import java.util.List; public class ServiceDetector { // this method is very important public boolean isServiceRunning(Context context, Class<?> serviceClass) { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE); if (services != null) { for (int i = 0; i < services.size(); i++) { if ((serviceClass.getName()).equals(services.get(i).service.getClassName()) && services.get(i).pid != 0) { return true; } } } return false; } } 

というクラスを持っています。 Androidと同じではありません。 –

+0

@VladMatvienkoはこれにはどんな解決策もあります –

+0

私は個人的に私の問題ではないと思います。私たちは、奇妙で馬鹿なMIUIの変化に苦しんでいる人ではありません。私が与えることができる唯一の提案は、アラームマネージャが実行しているサービスを5秒ごとにチェックし、ダウンしている場合はそれを開始することです。 –

答えて

1

実装できる回避策は、削除するたびにサービスを再開することです。つまり、onTaskRemovedコールバック(link)を使用します。この問題については

@Override 
public void onTaskRemoved(Intent rootIntent) { 
    // TODO Auto-generated method stub 
    Intent restartService = new Intent(getApplicationContext(), 
      this.getClass()); 
    restartService.setPackage(getPackageName()); 
    PendingIntent restartServicePI = PendingIntent.getService(
      getApplicationContext(), 1, restartService, 
      PendingIntent.FLAG_ONE_SHOT); 
    AlarmManager alarmService = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE); 
    alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() +1000, restartServicePI); 

} 
3

、私は様々なイベントに私のTestServiceのために多くのチェックポイント すなわち

  1. NetworkChangeReceiver
  2. BootReceiver
  3. MainActivity
ののonCreateメソッドを作ります

は、私はあなたのサービスがリラックス、それはMIUIだ再び

public void onReceive(Context context, Intent intent) { 
     ServiceDetector serviceDetector = new ServiceDetector(); 
     if (!serviceDetector.isServiceRunning(context, TestService.class)) { 
      Intent startServiceIntent = new Intent(context, TestService.class); 
      context.startService(startServiceIntent); 
     } else { 
      Log.i(Constants.TAG, "Service is already running reboot"); 
     } 
    } 
関連する問題