在应用程序启动时打开GPS

问题描述 投票:-3回答:4

我已经开始在Android上使用GoogleMap了,我希望我的应用程序在应用程序启动时自动打开GPS,并在应用程序关闭或用户退出应用程序时关闭。

如何完成上述任务?

android google-maps gps
4个回答
2
投票

Android应用无法自动启用GPS。它需要用户的身份验证才能这样做。

您可以做的最好的事情是有一个弹出对话框,要求用户允许访问位置服务。


3
投票

您可以提示用户打开GPS,如:

private void buildAlertMessageNoGps() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(
                this.getActivity());
        builder.setMessage(R.string.gps_disabled)
                .setCancelable(false)
                .setTitle(R.string.gps_disabled_title)
                .setPositiveButton(R.string.enable,
                        new DialogInterface.OnClickListener() {
                            public void onClick(
                                    @SuppressWarnings("unused") final DialogInterface dialog,
                                    @SuppressWarnings("unused") final int id) {
                                startActivity(new Intent(
                                        android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                            }
                        })
                .setNegativeButton(R.string.cancel,
                        new DialogInterface.OnClickListener() {
                            public void onClick(final DialogInterface dialog,
                                    @SuppressWarnings("unused") final int id) {
                                dialog.cancel();

                            }
                        });
        final AlertDialog alert = builder.create();
        alert.show();
    }

1
投票

不,不可能也不合适。没有他的权限,你不能只管理用户电话。

来自Play商店:

“当你试图本地化你的设备时,Cerberus会自动启用GPS(仅限Android <2.3.3),你可以保护它免受未经授权的卸载 - 应用程序配置中的更多信息。”

你可以这样做:

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

0
投票

完全解决方案在这里。

AndroidManifest.xml中

可能你需要以下权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

MainActivity代码

public class MainActivity extends AppCompatActivity {

Context context;

  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //... some code to init Activity and etc...

        context = this;

        LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE );
        boolean statusOfGPS = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        if(!statusOfGPS) // Before show message to turn on GPS be sure it is turned off.
        {
            buildAlertMessageNoGps();
        }
    }



private void buildAlertMessageNoGps() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(
                this.getActivity());
        builder.setMessage(R.string.gps_disabled)
                .setCancelable(false)
                .setTitle(R.string.gps_disabled_title)
                .setPositiveButton(R.string.enable,
                        new DialogInterface.OnClickListener() {
                            public void onClick(
                                    @SuppressWarnings("unused") final DialogInterface dialog,
                                    @SuppressWarnings("unused") final int id) {
                                startActivity(new Intent(
                                        android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                            }
                        })
                .setNegativeButton(R.string.cancel,
                        new DialogInterface.OnClickListener() {
                            public void onClick(final DialogInterface dialog,
                                    @SuppressWarnings("unused") final int id) {
                                dialog.cancel();

                            }
                        });
        final AlertDialog alert = builder.create();
        alert.show();
    }
© www.soinside.com 2019 - 2024. All rights reserved.