如何注销监听器&功放;从广播接收器内的一站式服务 [英] How to unregister Listener & stop service from within broadcastreceiver

查看:236
本文介绍了如何注销监听器&功放;从广播接收器内的一站式服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我有时在SMS消息接收的关键字,它启动跟踪电话的GPS位置的服务的广播接收机。我做到这一点使用 -

  context.startService(新意图(背景下,TrackGPS.class));

我还需要能够对在短信接收另一个关键字停止服务,我试图做到这一点,但GPS传感器仍然在屏幕顶部跟踪定位和GPS图标闪烁。我试图用这个做 -

  context.stopService(新意图(背景下,TrackGPS.class));

我想这可能是因为GPS监听器必须注册。谁能帮助我得到这个服务停止+停止GPS在收到一个文本的跟踪?提前致谢。

解决方案

  @覆盖
公众诠释onStartCommand(意向GPSService,诠释标志诠释startId){
    布尔startListening = GPSService.getExtras()getBoolean(IsStartTracking);
    如果(startListening ==真){
        LM =(的LocationManager)getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,这一点);
    }
    其他{
        lm.removeUpdates(本);
        stopSelf();
    }
    返回1;
}


解决方案

我不认为你的问题是停止服务。通过杀死你的服务,你不杀死还在寻找更新LocationListener的对象。

我要做到以下几点。

而不是停止和启动您的服务,只需每次你需要做的事情(开始收听或停止收听)时间启动服务。

添加一个额外的给你用它来告诉你,你想要做什么意图,是这样的:

 意图GPSService =新意图(背景下,TrackGPS.class);
GPSService.putExtra(IsStartTracking,真正的);
context.startService(GPSService);

在你的服务你的OnStart方法覆盖,您现在可以注册并依据IsStartTracking

的价值你的LocationManager的一个实例注销位置监听器

 布尔startListening = intent.getExtras()getBoolean(IsStartTracking);

In my App I have a broadcast receiver that upon receiving a keyword in an SMS message, it starts a service that tracks the GPS location of the phone. I do this using -

context.startService(new Intent(context,TrackGPS.class));

I also need to be able to stop the service upon receiving another keyword in an SMS message, I have tried to do this but the GPS sensor still tracks the location and GPS icon flashes at the top of the screen. I tried to do this using -

context.stopService(new Intent(context, TrackGPS.class));

I figure this might be because the GPS listener needs to be unregistered. Can anyone help me in getting this service to stop + stop the GPS tracking upon receipt of a text? Thanks in advance.

Solution

@Override
public int onStartCommand(Intent GPSService, int flags, int startId) {
    boolean startListening = GPSService.getExtras().getBoolean("IsStartTracking");
    if (startListening == true){
        lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    }
    else {
        lm.removeUpdates(this); 
        stopSelf();
    }
    return 1;
}

解决方案

I don't think your issue is stopping the service. By killing your service you are not killing the LocationListener object that is still looking for updates.

I would do the following.

Instead of stopping and starting your services, simply start your service every time you need to do something (Start listening or stop listening).

Add an extra to the Intent you use to tell you what you want to do, something like:

Intent GPSService = new Intent(context, TrackGPS.class);
GPSService.putExtra("IsStartTracking", true);
context.startService(GPSService);   

In your onStart method override of your service, you can now register and unregister location listeners on a single instance of your LocationManager based on the value of "IsStartTracking"

boolean startListening = intent.getExtras().getBoolean("IsStartTracking");

这篇关于如何注销监听器&功放;从广播接收器内的一站式服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆