从最近的应用程序中删除应用程序后,IntentService停止工作

问题描述:

我正在我的应用程序中使用地理围栏,并且基于地理围栏事件(Enter或Exit),我想执行一些操作.地理围栏文档说,一旦设置了地理围栏,它将自动触发事件,您可以使用IntentService捕获此事件.为此,我做了如下的intentservice:

I am using geofence in my app and based on geofence events (Enter or Exit) I want to perform some action. Geofence documentation says that once you set geofence it will trigger events automatically and you can catch this events with IntentService. For that I have made intentservice as below:

GeofenceTransitionsIntentService.java

public class GeofenceTransitionsIntentService extends IntentService {

    Handler mHandler;
    public GeofenceTransitionsIntentService() {
        super("GeofenceTransitionsIntentService");
        mHandler = new Handler();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("JK-->>","service started!");
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        Log.e("JK-->>","onHandel--->>");

        GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
        if (geofencingEvent.hasError()) {
            Log.e("JK-->>","geofenceEvent has error!");
            return;
        }

        int geofenceTransitionType = geofencingEvent.getGeofenceTransition();
        if (geofenceTransitionType == Geofence.GEOFENCE_TRANSITION_ENTER) {
            Log.e("JK-->>","enter!");
            mHandler.post(new DisplayToast(this,"Enter"));
        } else if (geofenceTransitionType == Geofence.GEOFENCE_TRANSITION_EXIT) {
            mHandler.post(new DisplayToast(this,"Exit"));
            Log.e("JK-->>","exit");
        }
    }

    public class DisplayToast implements Runnable {
        private final Context mContext;
        String mText;

        public DisplayToast(Context mContext, String text){
            this.mContext = mContext;
            mText = text;
        }

        public void run(){
            Toast.makeText(mContext, mText, Toast.LENGTH_SHORT).show();
        }
    }

}

现在,问题是,当应用打开(无论是前景还是背景)并且我在地理围栏中进入或退出时,它都能正常工作并向我显示祝酒消息,logcat显示日志,但是当我从最近的应用中删除应用时,没有向我显示吐司消息,或者在logcat中没有显示日志.

Now, problem is that when app is open(No matter foreground or background) and I enter or exit in geofence it works fine and show me a toast message and logcat shows log but when I remove app from recent apps there is no toast message showing to me or no log is showing in logcat.

我试图在Google上找到解决方案,但是大多数答案都建议使用该服务,但是如果我没记错,那么IntentService在工作完成后会自动停止运行,并在收到任何意图后自动启动.因此,我认为使用IntentService来完成此任务更为有效.

I have tried to find solution on google but mostly all answers suggests to use the service but if i am not wrong then IntentService stops itself automatically after work is done and start itself when any intent received. So, I think it's more efficient to use IntentService to do this task.

更新 我正在使用以下代码行注册geofence.

UPDATE I am registering geofence using following line of code.

geofencingClient.addGeofences(getGeofencingRequest(),getGeofencePendingIntent());

在getGeofencePendingIntent()中,我正在使用以下代码行启动意图服务.

and in getGeofencePendingIntent() i am starting intent service using following line of code.

private PendingIntent getGeofencePendingIntent() {
        if(geofencePendingIntent != null)
            return geofencePendingIntent;
        Intent in = new Intent(SetProfileOnlineActivity.this,GeofenceTransitionsIntentService.class);
        geofencePendingIntent = PendingIntent.getService(SetProfileOnlineActivity.this,111451,in,PendingIntent.FLAG_UPDATE_CURRENT);
        return geofencePendingIntent;
    }

我找到了答案...我的代码没有问题,IntentService也运行得很好,但是错误正在测试中强>.我正在android Oreo运行设备上测试我的应用程序.

I had found an answer... there was no problem in my code and IntentService was also working perfectly but the mistake was in the testing. I was testing my application on android Oreo running device.

在android oreo中,谷歌已更新其政策,即在前台他们将发送位置更新任何次数,但在后台中,他们将仅在小时内发送几次位置更新 . 节省设备使用寿命的主要原因.

In android oreo google has updated their policy that in foreground they will send location updates any number of times but in background they will send location updates only few times in hour. The main reason behind it to save the bettery life of device.

有关android oreo位置更新的更多信息,您可以查看文档.

For more information about android oreo location updates you can check out this documentation.