在应用程序销毁时,Android正在重新创建服务
这已经被问了很多次了,我已经看到并应用了很多答案,但在这里似乎没有任何帮助 这就是我在应用程序启动时启动服务器的方式在应用程序销毁时,Android正在重新创建服务,android,performance,android-layout,android-fragments,android-service,Android,Performance,Android Layout,Android Fragments,Android Service,这已经被问了很多次了,我已经看到并应用了很多答案,但在这里似乎没有任何帮助 这就是我在应用程序启动时启动服务器的方式 startService(new Intent(MavsMainActivity.this, LocationUpdateService.class)); 清单 <service android:name=".myservices.services.LocationUpdateService" android:process=":locationService"
startService(new Intent(MavsMainActivity.this, LocationUpdateService.class));
清单
<service android:name=".myservices.services.LocationUpdateService"
android:process=":locationService"
/>
您必须使用
IntentService
而不是Service
为什么
服务:该服务在后台运行,但在应用程序的主线程上运行
@Override
public void onCreate() {
if (isGooglePlayServicesAvailable()) {
/* min dist for location change, here it is 10 meter */
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
Toast.makeText(getApplicationContext(), "Service created", Toast.LENGTH_SHORT).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand: ");
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(BACKGROUND_INTERVAL);
mLocationRequest.setFastestInterval(30000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
mLocationRequest.setSmallestDisplacement(20);
sendOfflineData();
Toast.makeText(getApplicationContext(), "onStart Called", Toast.LENGTH_SHORT).show();
return START_STICKY ;
}
IntentService:IntentService在单独的工作线程上运行
在应用程序销毁服务时,如果返回START\u STICKY
,则停止并重新启动它,因为它的线程是独立的,但在主应用程序线程上运行
您应该
IntentService
并在此处查找。您必须使用IntentService
而不是Service
为什么
服务:该服务在后台运行,但在应用程序的主线程上运行
@Override
public void onCreate() {
if (isGooglePlayServicesAvailable()) {
/* min dist for location change, here it is 10 meter */
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
Toast.makeText(getApplicationContext(), "Service created", Toast.LENGTH_SHORT).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand: ");
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(BACKGROUND_INTERVAL);
mLocationRequest.setFastestInterval(30000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
mLocationRequest.setSmallestDisplacement(20);
sendOfflineData();
Toast.makeText(getApplicationContext(), "onStart Called", Toast.LENGTH_SHORT).show();
return START_STICKY ;
}
IntentService:IntentService在单独的工作线程上运行
在应用程序销毁服务时,如果返回START\u STICKY
,则停止并重新启动它,因为它的线程是独立的,但在主应用程序线程上运行
您应该
IntentService
并在此处查找。使用IBinder继续您的后台进程感谢在清单文件中添加进程后应用程序崩溃您不能这样做。这是安卓设计。当您的应用程序被系统要求内存或用户本身杀死时,系统将重新启动您的服务。请使用IBinder继续您的后台进程感谢在清单文件中添加进程后应用程序崩溃您不能这样做。这是安卓设计。当您的应用程序被系统占用内存或被用户本身杀死时,系统将重新启动您的服务。谢谢,这让事情变得更清楚,如果您在此处回答一个与此问题无关的问题,我们将不胜感激。我从IntentService扩展了服务,它创建了两个额外的方法,onHandleIntent
和一个构造函数,我需要在它们中做些什么吗,或者在不给这些方法任何实现的情况下它能正常工作。在IntentService的情况下,将您的逻辑(代码)实现到onHandleIntent方法中。谢谢,这让事情更清楚了,如果您在这里回答一个与此问题无关的问题,我们将不胜感激。我从IntentService扩展了服务,它创建了两个额外的方法,onHandleIntent
和一个构造函数,我是否需要在它们中执行任何操作,或者在不给这些方法任何实现的情况下它是否可以正常工作。如果是IntentService,请将您的逻辑(代码)实现到onHandleIntent方法中。