android 前後臺保活 實現定位數據定時上傳並展示軌跡 (上)

android 後臺保活我大該使用了下面幾種:
1.雙進程 拉起 —6.0以下
2. JobService --6.0以上
3. 1像素保活 — 怎麼說呢,感覺是7.0以下。適應度低。(問題多,就沒使用了)
4. 無限保活音樂 ---- 效果最好,但是呢耗電。由於我項目特殊性,也採用了。

還使用了前臺服務。 所以效果還是可以了,被殺死情況還算少。但是呢,也是很耗電的。
由於我項目特殊,是給專門的人使用,還會給他們配上充電器。所以就不管了。
下面我說下我怎麼實現的。 1像素保活,由於有時候監聽不到部分系統的,會閃退等
問題就沒有使用了。 最後給大家加上,提供參考。

先說下大概的類:
DownloadService :我們工作的類,也就是爲了保活這個service.

GuardService; 守護服務
StepService:主服務
這倆個是雙進程 拉起

PlayerMusicService: 無限播放音樂,後面爲了好控制關閉,就在工作類中使用無限播放音樂
ScheduleService: JobService 保活方式

ServiceAliveUtils:判斷工作服務是否還活着的工作類
public class ServiceAliveUtils {
public static boolean isServiceAlice() {
boolean isServiceRunning = false;
ActivityManager manager = (ActivityManager) MyApption.getMyApplication().getSystemService(Context.ACTIVITY_SERVICE);

    if (manager == null) {
        return true;
    }
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
    
        if (" com.example.admin.linyuandome.houtaibaohuo.DownloadService".equals(service.service.getClassName())) {
            isServiceRunning = true;
        }
    }
    return isServiceRunning;
}

}
雙進程:
/**

  • 守護進程 雙進程通訊

  • @author LiGuangMin

  • @time Created by 2018/8/17 11:27
    */
    public class GuardService extends Service {
    private final static String TAG = GuardService.class.getSimpleName();
    private ServiceConnection mServiceConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
    //Logger.d(TAG, “GuardService:建立鏈接”);
    boolean isServiceRunning = ServiceAliveUtils.isServiceAlice();
    if (!isServiceRunning) {
    Intent i = new Intent(GuardService.this, DownloadService.class);
    startService(i);
    }
    }

     @Override
     public void onServiceDisconnected(ComponentName componentName) {
         // 斷開鏈接
         startService(new Intent(GuardService.this, StepService.class));
         // 重新綁定
         bindService(new Intent(GuardService.this, StepService.class), mServiceConnection, Context.BIND_IMPORTANT);
     }
    

    };

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
    return new KeepAliveConnection.Stub() {
    @Override
    public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

         }
     };
    

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    startForeground(1, new Notification());
    // 綁定建立鏈接
    bindService(new Intent(this, StepService.class), mServiceConnection, Context.BIND_IMPORTANT);
    return START_STICKY;
    }

}

/*
主進程
*/
public class StepService extends Service {
private final static String TAG = StepService.class.getSimpleName();
private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
//Logger.d(TAG, “StepService:建立鏈接”);
boolean isServiceRunning = ServiceAliveUtils.isServiceAlice();
if (!isServiceRunning) {
Intent i = new Intent(StepService.this, DownloadService.class);
startService(i);
}
}

    @Override
    public void onServiceDisconnected(ComponentName componentName) {
        // 斷開鏈接
        startService(new Intent(StepService.this, GuardService.class));
        // 重新綁定
        bindService(new Intent(StepService.this, GuardService.class), mServiceConnection, Context.BIND_IMPORTANT);
    }
};

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return new KeepAliveConnection.Stub() {
        @Override
        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

        }
    };
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    startForeground(1, new Notification());
    // 綁定建立鏈接
    bindService(new Intent(this, GuardService.class), mServiceConnection, Context.BIND_IMPORTANT);
    return START_STICKY;
}

}

無限播放音樂:
/*
無限播放音樂
*/
public class PlayerMusicService extends Service {
private final static String TAG = PlayerMusicService.class.getSimpleName();
private MediaPlayer mMediaPlayer;

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
   // Logger.d(TAG, TAG + "---->onCreate,啓動服務");
    mMediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.no_notice);
    mMediaPlayer.setLooping(true);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            startPlayMusic();
        }
    }).start();
    return START_STICKY;
}

private void startPlayMusic() {
    if (mMediaPlayer != null) {
        mMediaPlayer.start();
    }
}

private void stopPlayMusic() {
    if (mMediaPlayer != null) {
        mMediaPlayer.stop();
    }
}

@Override
public void onDestroy() {
    super.onDestroy();
    stopPlayMusic();
    // 重啓自己
    Intent intent = new Intent(getApplicationContext(), PlayerMusicService.class);
    startService(intent);
}

}

/*
JobService
*/
@SuppressLint(“NewApi”)
public class ScheduleService extends JobService {
private static final String TAG = ScheduleService.class.getSimpleName();

@Override
public boolean onStartJob(JobParameters params) {

    boolean isServiceRunning = ServiceAliveUtils.isServiceAlice();
    if (!isServiceRunning) {
        Intent i = new Intent(this, DownloadService.class);
        startService(i);
        
     //   Logger.d(TAG, "ScheduleService啓動了DownloadService");
    }
    jobFinished(params, false);
    return false;
}

@Override
public boolean onStopJob(JobParameters params) {
    return false;
}

}

啓動所有保活服務和工作服務
Intent intent = new Intent(mContext, DownloadService.class);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// 6.0 以上 jobservice
useJobServiceForKeepAlive();
}
//雙守護線程
startService(new Intent(mContext, StepService.class));
startService(new Intent(mContext, GuardService.class));
// 無限播放
startService(new Intent(mContext, PlayerMusicService.class));

關閉就不說了。後面說

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章