我建立一个基于位置的应用程序,我已经创建了在后台工作的服务。当应用程序在云背景或销毁,在我的服务类,如果我发挥循环音频它完美的作品。
音频播放继续连我破坏应用程序,用于获取用户的位置我使用FusedLocationProviderClient API,它也从服务的工作,但只有当应用程序在前台当应用程序用户转到其他应用程序或毁坏App这个它没有得到的位置,但媒体播放器在相同的服务工作正常。
我的服务类
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;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句