以编程方式启用GPS不工作



我正试图通过点击按钮在我的应用程序中以编程方式启用/禁用(切换)GPS,但它不起作用。请帮忙。我的目标设备没有根。

这是我正在使用的代码。

private void gps()
{
Intent intent=new Intent("android.location.GPS_ENABLED_CHANGE");
Button gps = (Button)findViewById(R.id.gpsButton);
if(isGPSon())
    {
    intent.putExtra("enabled", true);
    gps.setText(R.string.f2_gps_deact);
    }
else
    {
    intent.putExtra("enabled", false);
    gps.setText(R.string.f2_gps_act);
    }
sendBroadcast(intent);

您无法通过程序启用GPS。您所能做的最多的事情就是打开用户的设置。

你可以这样做:

startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 100);

我正试图通过点击按钮在我的应用程序中以编程方式启用/禁用(切换)GPS

出于明显的隐私原因,这在现代版本的安卓系统上是不可能的。

但不起作用

Android SDK中没有android.location.GPS_ENABLED_CHANGE操作。有一个在内部使用,但它只是宣布状态更改——实际上并没有更改状态本身。

// location manager

LocationManager位置管理器;LocationListener LocationListener;

obj_tgl_gps.setOnClickListener(新的OnClickListenr(){

        public void onClick(View v)
        {
            if (obj_tgl_gps.isChecked())
            {
                Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
                intent.putExtra("enabled", true);
                sendBroadcast(intent);
            }
            else
            {
                Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
                intent.putExtra("enabled", false);
                sendBroadcast(intent);
            }
        }
    });

最新更新