意图打开GPS并返回应用程序

时间:2015-10-16 14:24:37

标签: android android-intent gps

此代码可以进入GPS设置并将其打开,但在按下时它不会意图返回应用程序。相反,它需要我到我的发射器的家。

        public void createDialog(){
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setTitle(R.string.gps_message);
        alert.setPositiveButton(R.string.gps_settings, new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
                return;
            }
        });

        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                return;
            }
        });
        alert.create();
        alert.show();
    }

2 个答案:

答案 0 :(得分:2)

尝试将您的开始代码更改为:

startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));

也可以在尝试将其更改为此后调用return:

dialog.dismiss();

致电返回可能会关闭您的活动。

答案 1 :(得分:0)

这对我有用:

AlertDialog.Builder notifyLocationServices = new AlertDialog.Builder(PlacesDecoder.this);
        notifyLocationServices.setTitle("Switch on Location Services");
        notifyLocationServices.setMessage("Location Services must be turned on to complete this action. Also please take note that if on a very weak network connection,  such as 'E' Mobile Data or 'Very weak Wifi-Connections' it may take even 15 mins to load. If on a very weak network connection as stated above, location returned to application may be null or nothing and cause the application to crash.");
        notifyLocationServices.setPositiveButton("Ok, Open Settings", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent openLocationSettings = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                PlacesDecoder.this.startActivity(openLocationSettings);
                finish();
            }
        });
        notifyLocationServices.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        notifyLocationServices.show();

注意:更改值以适合自己。 也知道这不会对结果使用start活动。所以请相应地修改它们。例如:类名 我希望这有帮助