简体   繁体   English

如何通过单击按钮打开和关闭GPS?

[英]How to switch the GPS on and off by using a button click?

i going to make an assistive touch for the android like ios app so i want to add the feature of gps on off there for this. 我将为ios之类的android做出辅助触摸,因此我想为此添加gps功能。 i want to turn on and turn off the gps using a click of image view when i click on it.It goes to the settings and from there you can on and off the location of your phone but i want that not going to setting when i click location is on and click other to off the location. 我想在单击图像时单击图像视图来打开和关闭gps。它转到设置,从那里可以打开和关闭手机的位置,但是我希望在我打开时不进行设置单击“打开位置”,然后单击“其他”以关闭该位置。

final ImageView locationOn = view.findViewById(R.id.location);
final ImageView locationOff = view.findViewById(R.id.locationOff); 

locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
        GpsStatus = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        if(GpsStatus) {
            locationOn.setVisibility(View.INVISIBLE);
            locationOff.setVisibility(View.VISIBLE);
        }
        else{
            locationOn.setVisibility(View.VISIBLE);
            locationOff.setVisibility(View.INVISIBLE);
        }
        locationOn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                locationOn.setVisibility(View.INVISIBLE);
                locationOff.setVisibility(View.VISIBLE);
            }
        });
        locationOff.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                locationOn.setVisibility(View.VISIBLE);
                locationOff.setVisibility(View.INVISIBLE);
            }
        });

when i click on it.It goes to the settings and from there you can on and off the location of your phone but i want that not going to setting when i click location is on and click other to off the location. 当我单击它时,它转到设置,从那里可以打开和关闭手机的位置,但是当我单击打开位置并单击其他以关闭位置时,我希望不进行设置。

No it't impossible, and inappropriate. 不,这不是不可能,而且不合适。 You can't just manage the users phone without his authority. 未经用户授权,您不能只管理用户的电话。

From Play Store: 从Play商店:

"Cerberus automatically enables GPS if it is off when you try to localize your device (only on Android < 2.3.3) and you can protect it from unauthorized uninstalling - more info in the app configuration." “当您尝试对设备进行本地化时(仅在Android <2.3.3上),Cerberus会自动启用GPS,并且可以保护它免受未经授权的卸载-应用程序配置中的更多信息。”

You can do something like this: 您可以执行以下操作:

startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));

By using Intents if API version is not latest 如果API版本不是最新的,则使用Intents

Enable GPS 启用GPS

Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
sendBroadcast(intent);

Disable GPS 停用GPS

Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
    intent.putExtra("enabled", false);
    sendBroadcast(intent);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM