简体   繁体   English

当应用程序被破坏或进入后台时,Android 位置服务停止工作

[英]Android location service stop working when app destroyed or goes in background

I am building a location based application and I have created a service which works in background.我正在构建一个基于位置的应用程序,并且创建了一个在后台运行的服务。 When app goes in background or destroyed and in my service class if I play audio in loop it works perfectly.当应用程序进入后台或被破坏时,如果我循环播放音频,它会在我的服务类中完美运行。

Audio is playing continue even I destroy app and for getting user location I am using FusedLocationProviderClient API and it also works from service but only when app is in foreground when app user goes to other app or destroys app this it's not getting location but media player is working fine in same service.即使我销毁应用程序和获取用户位置,音频仍在继续播放我正在使用FusedLocationProviderClient API 并且它也可以从服务中工作但仅当应用程序用户转到其他应用程序或销毁应用程序时应用程序处于前台时它不会获取位置但媒体播放器是在相同的服务中工作正常。

My Service Class我的服务类

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;
    }
}

According to the documentation :根据文档

This method is suited for the foreground use cases. For background use cases, the PendingIntent version of the method is recommended, see requestLocationUpdates(LocationRequest, PendingIntent).

Take a look at this sample project with PendingIntent .看看这个带有PendingIntent 示例项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM