【问题标题】:Start android app when GPS is used使用 GPS 时启动 android 应用
【发布时间】:2013-11-21 05:50:27
【问题描述】:

我想让我的服务在 GPS 被任何应用程序使用时自动启动,并在 GPS 不再使用时停止。如果可能,我想避免让我的服务在后台运行。

显然,“GPS 已激活”没有广播意图,例如“启动完成”。那将是完美的开始我的服务。我的另一个想法是在 LocationManager 上注册一个 GpsStatus.Listener,但我认为我的应用程序必须一直在后台运行(并且需要自动启动权限)。

我之所以要这样做,是因为我想在 GPS 使用时收集它的状态信息。

感谢您的想法!

【问题讨论】:

  • 没有这样的方法,您必须为此运行至少1个后台服务...

标签: android gps


【解决方案1】:

你可以这样做

 @Override 
    public void onCreate(Bundle savedInstanceSate){
                super.onCreate(savedInstaceState);
                setContentView(R.layout.main);
                //Location methods
                locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                locationListener = new GpsLocationListener();
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    }
    class GpsLocationListener implements LocationListener {
            @Override
            public void onLocationChanged(Location location) {
            **write  here methods for services when your GPS is active or working**
                Toast.makeText(context, "Location Data Updated", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onProviderDisabled(String provider) {
                Toast.makeText(context, "Gps Disabled", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onProviderEnabled(String provider) {
                Toast.makeText(context, "Gps Enabled", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
            }

        }

【讨论】:

  • 谢谢,但这需要服务自动启动才能注册监听器,对吧?我想避免这种情况。例如,this app 不需要该权限。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多