KKO
KKO

Reputation: 1933

Build an alert in a separate class to request for GPS enabling

I am trying to build an alert to tell the user that his GPS is disabled, and when pressing OK to be taken to the Settings to enable it. It is a class outside of MainActivity.

The problem is that I cannot start the activity based on Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS). I get:

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

I tried extending AlertDialogManager extends Activity, but then I get a NullPointerException error.

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)

Here is the source code:

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
        }
    }
}

If I build this alert in MainActivity it works ok, but I want it separate. How can I solve this?

Upvotes: 0

Views: 417

Answers (1)

A.Vynohradov
A.Vynohradov

Reputation: 276

You should register a BroadcastReceiver that listens to PROVIDERS_CHANGED_ACTION intent that is sent by the system when location provider state is changed. Receiver should be declared in your Manifest.xml file. When you receive the aforementioned intent try to check out if GPS provider state has changed and display your dialog accordingly.

Try following code:
// 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));

Upvotes: 1

Related Questions