机器人onStatusChanged不工作 [英] Androids onStatusChanged not working

查看:144
本文介绍了机器人onStatusChanged不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让GPS_PROVIDER变化,如果通知的状态。我发现下面的code在这里(http://hejp.co.uk/android/android-gps-example/),但我没有得到通知。

  1. 右键现在我在一个建筑物并不能得到GPS信号,所以不会我得到的通知状态更改:停止运行?
  2. 当onStatusChanged被称为?
  3. 我究竟做错了什么?

谢谢

  @覆盖
公共无效onStatusChanged(字符串商,INT地位,捆绑演员){
    / *这就是所谓的当GPS状态改变* /
    开关(状态){
    案例LocationProvider.OUT_OF_SERVICE:
        Log.v(标签,状态改变:停止运行);
        Toast.makeText(这一点,状态改变:不在服务区,
                Toast.LENGTH_SHORT).show();
        打破;
    案例LocationProvider.TEMPORARILY_UNAVAILABLE:
        Log.v(标签,状态改变:暂时不可用);
        Toast.makeText(这一点,状态改变:暂时不可用,
                Toast.LENGTH_SHORT).show();
        打破;
    案例LocationProvider.AVAILABLE:
        Log.v(标签,状态改变:用);
        Toast.makeText(这一点,状态更改:可用,
                Toast.LENGTH_SHORT).show();
        打破;
    }
 

解决方案

如果您的GPS状态没有发生变化(例如,如果你总是在室内无GPS定位)的应用程序运行时,有些设备不触发 OnStatusChanged()方法。

如果您的应用程序运行时更改GPS状态(例如,你在里面,并不能得到修复,然后走到外面,并能得到修复,或者反之),那么 OnStatusChanged()方法应该火在所有设备上。

如果你想有一个完全工作的开源应用程序来作为例子使用,尽量GPSTest(完全公开,我的应用程序):

GPSTest在谷歌播放 - <一个href="https://play.google.com/store/apps/details?id=com.android.gpstest">https://play.google.com/store/apps/details?id=com.android.gpstest

来源$ C ​​$下GPSTest - https://github.com/barbeau/gpstest

有关全球定位系统的不断更新,即使您的设备无法得到修复,你可能要注册的详细信息 GPSStatus.Listener

在你的活动,使之落实 GpsStatus.Listener ,例如:

 公共类GpsTestActivity扩展TabActivity
    实现LocationListener的,GpsStatus.Listener {
 

然后,在你的活动声明类变量:

 私人LocationManager MSERVICE;
私人GpsStatus MSTATUS;
 

...并添加方法来处理GPSStatus变化:

 公共无效onGpsStatusChanged(INT事件){
    MSTATUS = mService.getGpsStatus(MSTATUS);
    开关(事件){
        案例GpsStatus.GPS_EVENT_STARTED:
            //做些什么MSTATUS信息
            打破;

        案例GpsStatus.GPS_EVENT_STOPPED:
            //做些什么MSTATUS信息
            打破;

        案例GpsStatus.GPS_EVENT_FIRST_FIX:
            //做些什么MSTATUS信息
            打破;

        案例GpsStatus.GPS_EVENT_SATELLITE_STATUS:
            //做些什么MSTATUS信息
            打破;
    }

}
 

然后在的OnCreate()的活动注册GPSStatus.Listener的:

  MSERVICE =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
 mService.addGpsStatusListener(本);
 

在GPSTest应用程序,当前可用卫星的列表显示与每个GPSStatus.Listener更新在屏幕上,在此基础上code:

<一个href="https://github.com/barbeau/gpstest/blob/master/GPSTest/src/main/java/com/android/gpstest/GpsStatusFragment.java">https://github.com/barbeau/gpstest/blob/master/GPSTest/src/main/java/com/android/gpstest/GpsStatusFragment.java

这样一来,您会收到系统的GPS状态主动更新,即使您的手机无法获得GPS定位(因此可能不会触发 OnStatusChanged 的该LocationListener的)。

I'm trying to get notifications if the status of GPS_PROVIDER changes. I found the following code here (http://hejp.co.uk/android/android-gps-example/), but I'm not getting notifications.

  1. Right now I'm in a building and can't get GPS signal, so wouldn't I get the notification "Status Changed: Out of Service"?
  2. When is onStatusChanged being called?
  3. What am I doing wrong?

Thanks,

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    /* This is called when the GPS status alters */
    switch (status) {
    case LocationProvider.OUT_OF_SERVICE:
        Log.v(tag, "Status Changed: Out of Service");
        Toast.makeText(this, "Status Changed: Out of Service",
                Toast.LENGTH_SHORT).show();
        break;
    case LocationProvider.TEMPORARILY_UNAVAILABLE:
        Log.v(tag, "Status Changed: Temporarily Unavailable");
        Toast.makeText(this, "Status Changed: Temporarily Unavailable",
                Toast.LENGTH_SHORT).show();
        break;
    case LocationProvider.AVAILABLE:
        Log.v(tag, "Status Changed: Available");
        Toast.makeText(this, "Status Changed: Available",
                Toast.LENGTH_SHORT).show();
        break;
    }

解决方案

If your GPS status isn't changing (e.g., if you're always indoors without a GPS fix) while the app is running, some devices won't trigger the OnStatusChanged() method.

If you change GPS statuses while the app is running (e.g., you're inside and can't get a fix and then walk outside and can get a fix, or vice versa), then the OnStatusChanged() method should fire on all devices.

If you want a fully working open-source app to use as an example, try GPSTest (full disclosure, my app):

GPSTest on Google Play - https://play.google.com/store/apps/details?id=com.android.gpstest

Source code for GPSTest - https://github.com/barbeau/gpstest

For more detailed information about GPS that is constantly updated even if your device can't get a fix, you might want to register a GPSStatus.Listener.

In your Activity, make it implement GpsStatus.Listener, for example:

public class GpsTestActivity extends TabActivity
    implements LocationListener, GpsStatus.Listener{

Then, in your activity declare class variables:

private LocationManager mService;
private GpsStatus mStatus;

...and add the method to handle the GPSStatus changes:

public void onGpsStatusChanged(int event) {
    mStatus = mService.getGpsStatus(mStatus);
    switch (event) {
        case GpsStatus.GPS_EVENT_STARTED:
            // Do Something with mStatus info
            break;

        case GpsStatus.GPS_EVENT_STOPPED:
            // Do Something with mStatus info
            break;

        case GpsStatus.GPS_EVENT_FIRST_FIX:
            // Do Something with mStatus info
            break;

        case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
            // Do Something with mStatus info
            break;
    }

}

Then in OnCreate() of your Activity to register the GPSStatus.Listener:

 mService = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
 mService.addGpsStatusListener(this);

In the GPSTest app, the list of currently available satellites is shown on the screen with each GPSStatus.Listener update, based on this code:

https://github.com/barbeau/gpstest/blob/master/GPSTest/src/main/java/com/android/gpstest/GpsStatusFragment.java

This way, you'll receive active updates on the GPS status of system even if your phone can't get a GPS fix (and therefore may not trigger OnStatusChanged of the LocationListener).

这篇关于机器人onStatusChanged不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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