在单独的班级中建立警报以请求启用GPS

时间:2014-07-09 13:59:32

标签: android gps alert alertdialog

我正在尝试建立一个警报,告诉用户他的GPS已被禁用,按下确定以进入设置以启用它。它是MainActivity之外的一个类。

问题是我无法基于Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)启动活动。我明白了:

The method startActivityForResult(Intent, int) is undefined for the type new DialogInterface.OnClickListener(){}

我尝试扩展AlertDialogManager extends Activity,但后来我收到NullPointerException错误。

07-09 15:46:17.922: E/AndroidRuntime(17275): FATAL EXCEPTION: main
07-09 15:46:17.922: E/AndroidRuntime(17275): java.lang.NullPointerException
07-09 15:46:17.922: E/AndroidRuntime(17275):    at android.app.Activity.startActivityForResult(Activity.java:3464)
07-09 15:46:17.922: E/AndroidRuntime(17275):    at android.app.Activity.startActivityForResult(Activity.java:3425)
07-09 15:46:17.922: E/AndroidRuntime(17275):    at com.example.places.AlertDialogManager$1.onClick(AlertDialogManager.java:42)
07-09 15:46:17.922: E/AndroidRuntime(17275):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:185)
07-09 15:46:17.922: E/AndroidRuntime(17275):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 15:46:17.922: E/AndroidRuntime(17275):    at android.os.Looper.loop(Looper.java:137)
07-09 15:46:17.922: E/AndroidRuntime(17275):    at android.app.ActivityThread.main(ActivityThread.java:5419)
07-09 15:46:17.922: E/AndroidRuntime(17275):    at java.lang.reflect.Method.invokeNative(Native Method)
07-09 15:46:17.922: E/AndroidRuntime(17275):    at java.lang.reflect.Method.invoke(Method.java:525)
07-09 15:46:17.922: E/AndroidRuntime(17275):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
07-09 15:46:17.922: E/AndroidRuntime(17275):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
07-09 15:46:17.922: E/AndroidRuntime(17275):    at dalvik.system.NativeStart.main(Native Method)

以下是源代码:

public class AlertDialogManager {
    public void showAlertDialog(Context context, String title, String message, Boolean status) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setMessage(message);

        if (status != null)
            builder.setIcon((status) ? R.drawable.ic_launcher : R.drawable.ic_launcher);

        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        startActivityForResult(intent, 5);                          
            }
        });

        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        });

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

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 5 && resultCode == 0) {
            String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
            if (provider != null) {
                switch (provider.length()) {
                case 0:
                    // GPS still not enabled..
                    break;
                default:
                    Toast.makeText(this, "GPS is now enabled.", Toast.LENGTH_LONG).show();
                    break;
                }
            }
        } else {
            // the user did not enable his GPS
        }
    }
}

如果我在MainActivity中构建此警报,它可以正常工作,但我想将它分开。 我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

当位置提供程序状态发生更改时,您应该注册一个侦听系统发送的PROVIDERS_CHANGED_ACTION意图的BroadcastReceiver。应在 Manifest.xml 文件中声明Receiver。当您收到上述意图时,请尝试检查GPS提供商状态是否已更改并相应地显示您的对话框。

请尝试以下代码:
// declaring local Context reference
final Context context = context.getApplicationContext();
// use this reference to start prefered Activity
context.startActivity(new Intent(android.provider.Settings. ACTION_LOCATION_SOURCE_SETTINGS));