我的应用就像一个跟踪器。它保存用户位置,然后将其发送到服务器。 我使这台服务器前景和粘性。在三星s4一切正常,但在华为荣誉4c服务被杀死的速度与锁定屏幕一样快。而且,不调用像onDestroy()这样的方法。如果我将应用程序添加到"受保护的应用程序"它的寿命更长,但接近30-45分钟。有没有办法让我的服务保持活力?或者有没有办法在没有前台服务的情况下解决我的问题?我搜索了很多关于这个问题的帖子,但没有找到解决方案。
这是我的服务
public class LocationService extends Service {
private static final long TIME_BETWEEN_LOCATION_SEND_IN_MILLIS = 60 * 1000;
private static final long TIME_BETWEEN_LOCATION_UPDATES = 10 * 1000;
private List<Location> locations;
private int NOTIFICATION_ID = 31337;
private boolean isAlreadyRunning = false;
public boolean Continue = true;
private GoogleApiClient googleApiClient;
@Override
public void onCreate() {
super.onCreate();
GoogleApiClient.Builder builder = new GoogleApiClient.Builder(getApplicationContext());
googleApiClient = builder
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(@Nullable Bundle bundle) {
Log.wtf("lol", "connected");
LocationRequest locationRequest = new LocationRequest();
locationRequest.setInterval(TIME_BETWEEN_LOCATION_UPDATES);
locationRequest.setFastestInterval(TIME_BETWEEN_LOCATION_UPDATES);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
//noinspection MissingPermission
LocationServices.FusedLocationApi.requestLocationUpdates(
googleApiClient, locationRequest, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
locations.add(location);
}
});
}
@Override
public void onConnectionSuspended(int i) {
Log.wtf("lol", "connection suspended");
}
})
.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(
@NonNull ConnectionResult connectionResult
) {
Log.wtf("lol", "connection Failed");
}
})
.addApi(LocationServices.API)
.build();
locations = new ArrayList<>();
}
@Override
public void onLowMemory() {
super.onLowMemory();
Log.wtf("lol","service onlow");
}
@Override
public void onTrimMemory(int level) {
super.onTrimMemory(level);
Log.wtf("lol","service ontrim");
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
Log.wtf("lol","service ontaskremoved");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.wtf("lol", "service OnStartCommand");
if (!isAlreadyRunning) {
Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
resultIntent.setAction(Intent.ACTION_MAIN);
resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent resultPendingIntent = PendingIntent.getActivity(
this,
333,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setContentTitle("Title")
.setContentText("Explanation!")
.setSmallIcon(R.drawable.ic_notification)
.setContentIntent(resultPendingIntent);
Notification notification = builder.build();
startForeground(NOTIFICATION_ID, notification);
googleApiClient.connect();
isAlreadyRunning = true;
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (locations.size() > 0) {
sendLocationsAndClearList();
}
if(Continue){
handler.postDelayed(this,TIME_BETWEEN_LOCATION_SEND_IN_MILLIS);
}
}
},TIME_BETWEEN_LOCATION_SEND_IN_MILLIS);
}
return Service.START_STICKY;
}
@Override
public void onDestroy() {
Log.wtf("lol", "service onDestroy");
Continue = false;
googleApiClient.disconnect();
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
答案 0 :(得分:0)
您还必须创建CPU唤醒锁。 CPU可以拆分为低功耗模式,这将停止您的应用程序进程。如果CPU由于某种原因再次启动,它可能会恢复。
在Android文档中查看WAKE LOCK主题。