2

The app I'm creating needs internet connection. I am able to check if the internet is and turn on internet programmatically in android 4.2.2. But I'm not able to turn on automatically in lollipop. I searched a bit and found that the phone needs to be rooted to use setMobileDataEnabled and getMobileDataEnabled.

How do I redirect the user to the internet settings so he/she can turn it on. And after turning it on, how do I automatically redirect user back to my app.

I tried this based on a SO post:

                Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.setClassName(MainActivity.this,
                    "com.android.phone.NetworkSetting");
            startActivity(intent);

But I get this error android.content.ActivityNotFoundException: Unable to find explicit activity class {com.prematixsofs.taxiapp/com.android.phone.NetworkSetting}; have you declared this activity in your AndroidManifest.xml?

Abhi
  • 1,512
  • 2
  • 22
  • 46
  • Possible duplicate of [How to open System Data Usage Activity in android?](http://stackoverflow.com/questions/20489414/how-to-open-system-data-usage-activity-in-android) – Gavriel Jan 20 '16 at 12:20
  • @Gavriel. The answer in that post provides solution to both the questions. The questions are different. But the same screen shows both data usage and option to turn on internet. But I wanted to turn on internet. So maybe this shouldn't be marked as duplicate? – Abhi Jan 20 '16 at 12:27

4 Answers4

7
// either this:
    Intent settingsIntent = new Intent(Settings.ACTION_SETTINGS);
// or this:
    Intent settingsIntent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);

    activity.startActivityForResult(settingsIntent, 9003);
Gavriel
  • 18,880
  • 12
  • 68
  • 105
  • I want to redirect user to turn on 2g/3g mobile data – Abhi Jan 20 '16 at 07:18
  • 1
    @Abhi, You aksed about how to redirect the user to the internet settings....that´s what Gavriel shows You. You should make a dialog to inform the user and let him do the choice if he wants to turn on the internet before.... – Opiatefuchs Jan 20 '16 at 07:35
  • @Opiatefuchs Yes, that's what I'm planning on doing. And this is his edited answer. My comment was for his previous answer – Abhi Jan 20 '16 at 08:16
  • @Opiatefuchs can you look at my answer and tell me what to do? – Abhi Jan 20 '16 at 12:07
  • I think You should just do what Gavriel wrote. delete the "setClass()" method and use it exactly like the example above.... – Opiatefuchs Jan 20 '16 at 12:09
  • @Opiatefuchs But like I said in my answer, this does not lead me directly to the screen. I have to select from a list of options. Using `com.android.settings.Settings$DataUsageSummaryActivity` sends to the "data usage" screen from which the user can turn on internet. I'm confused about which one I should mark as correct – Abhi Jan 20 '16 at 12:15
  • 3rd option: close this question as a duplicate of: http://stackoverflow.com/questions/20489414/how-to-open-system-data-usage-activity-in-android – Gavriel Jan 20 '16 at 12:17
2

This is OP. The answers here do not directly send the user to the "turn on mobile data" activity of android. The user will have to select the "Mobile Network" from a list of other options. So I'm not sure if I should mark the answers as right. I don't know if this will work for all Lollipop devices, but this code sent me directly to the "turn on mobile data" screen in my device.

Here's the code:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("com.android.settings", "com.android.settings.Settings$DataUsageSummaryActivity");
startActivity(intent);
Abhi
  • 1,512
  • 2
  • 22
  • 46
  • caution: causes ActivityNotFoundException on some devices (my case was a customer's Xiaomi Redmi Note on android7). – mrahimygk Feb 11 '19 at 10:17
  • adding to above list, a Huawei P9 lite too. this was on adnroid7 too, so maybe the problem comes from the OS rather than the phones. – mrahimygk Feb 11 '19 at 10:20
0

Try this code

    btn = (Button)this.findViewById(R.id.ButtonNet);
btn.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent=new Intent(Settings.ACTION_WIRELESS_SETTINGS);
        startActivity(intent);
    }
});
Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84
0

You can try this:

public static AlertDialog displayMobileDataSettingsDialog(final Activity activity,final Context context, String title, String message){
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);
                context.startActivity(intent);
            }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
                activity.finish();

            }
        });
        builder.show();

        return builder.create();
    }

Hope it helps!!! Found in https://github.com/kostasdrakonakis/dialog/blob/master/Custom%20Dialog/DialogMessageDisplay.java

Kostas Drak
  • 3,222
  • 6
  • 28
  • 60