Java 当应用程序被破坏或进入后台时,Android定位服务将停止工作
我正在构建一个基于位置的应用程序,并创建了一个在后台工作的服务。当应用程序进入后台或被破坏时,若我在我的服务类中循环播放音频,那个么它工作得非常好 即使我销毁了应用程序,音频仍在继续播放。为了获取用户位置,我正在使用FusedLocationProviderClient API,它也可以从服务中工作,但只有当应用程序位于前台时,当应用程序用户转到其他应用程序或销毁应用程序时,它无法获取位置,但媒体播放器在同一服务中工作正常 我的服务班 根据报告: 此方法适用于前台用例。对于后台用例,建议使用该方法的PendingEvent版本,请参阅requestLocationUpdatesLocationRequest,PendingEvent 看一看悬挂式帐篷Java 当应用程序被破坏或进入后台时,Android定位服务将停止工作,java,android,Java,Android,我正在构建一个基于位置的应用程序,并创建了一个在后台工作的服务。当应用程序进入后台或被破坏时,若我在我的服务类中循环播放音频,那个么它工作得非常好 即使我销毁了应用程序,音频仍在继续播放。为了获取用户位置,我正在使用FusedLocationProviderClient API,它也可以从服务中工作,但只有当应用程序位于前台时,当应用程序用户转到其他应用程序或销毁应用程序时,它无法获取位置,但媒体播放器在同一服务中工作正常 我的服务班 根据报告: 此方法适用于前台用例。对于后台用例,建议使用该方
public class ServiceClass extends Service {
private MediaPlayer mediaplayer;
private FusedLocationProviderClient fusedLocationProviderClient;
private LocationRequest locationRequest;
private Context mContext;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
mContext = this;
mediaplayer = MediaPlayer.create(this,Settings.System.DEFAULT_RINGTONE_URI);
mediaplayer.setLooping(true);
mediaplayer.start();
getLocation();
return START_STICKY;
}
public void getLocation() {
Toast.makeText(mContext, "Engine X started YO", Toast.LENGTH_SHORT).show();
fusedLocationProviderClient = new FusedLocationProviderClient(this);
locationRequest = new LocationRequest();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setFastestInterval(1000);
locationRequest.setInterval(1000);
fusedLocationProviderClient.requestLocationUpdates(locationRequest, new LocationCallback(){
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
Log.d("X_loc_BAck", ""+locationResult.getLastLocation().getAccuracy());
Toast.makeText(mContext, ""+locationResult.getLastLocation().getLatitude(), Toast.LENGTH_SHORT).show();
}
},getMainLooper());
}
@Override
public void onDestroy() {
super.onDestroy();
mediaplayer.stop();
//Toast.makeText(this, "Service Desttroyed", Toast.LENGTH_SHORT).show();
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
mediaplayer.stop();
Intent i = new Intent(this, ServiceStopped.class);
sendBroadcast(i);
//Toast.makeText(this, "Service Task Removed", Toast.LENGTH_SHORT).show();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}