【问题标题】:Redirect user to turn on internet settings重定向用户以打开互联网设置
【发布时间】:2016-01-20 07:11:23
【问题描述】:

我正在创建的应用需要互联网连接。我能够检查互联网是否可用,并在 android 4.2.2 中以编程方式打开互联网。但我无法在棒棒糖中自动打开。我搜索了一下,发现手机需要root才能使用setMobileDataEnabledgetMobileDataEnabled

如何将用户重定向到互联网设置,以便他/她可以打开它。开启后,如何自动将用户重定向回我的应用。

我根据 SO 帖子尝试了这个:

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

但我收到此错误 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?

【问题讨论】:

  • @Gavriel。该帖子中的答案为这两个问题提供了解决方案。问题是不同的。但同一屏幕显示数据使用情况和打开互联网的选项。但我想打开互联网。所以也许这不应该被标记为重复?

标签: android android-intent android-5.0-lollipop


【解决方案1】:
// either this:
    Intent settingsIntent = new Intent(Settings.ACTION_SETTINGS);
// or this:
    Intent settingsIntent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);

    activity.startActivityForResult(settingsIntent, 9003);

【讨论】:

  • 我要重定向用户开启 2g/3g 移动数据
  • @Abhi,您询问了如何将用户重定向到互联网设置......这就是 Gavriel 向您展示的内容。如果他想在此之前打开互联网,您应该制作一个对话框通知用户并让他做出选择......
  • @Opiatefuchs 是的,这就是我打算做的事情。这是他编辑的答案。我的评论是针对他之前的回答
  • @Opiatefuchs 你能看看我的回答并告诉我该怎么做吗?
  • 我认为你应该按照 Gavriel 写的去做。删除“setClass()”方法并像上面的例子一样使用它......
【解决方案2】:

这是 OP。这里的答案不会直接将用户发送到android的“打开移动数据”活动。用户必须从其他选项列表中选择“移动网络”。所以我不确定我是否应该将答案标记为正确。我不知道这是否适用于所有 Lollipop 设备,但此代码直接将我发送到设备中的“打开移动数据”屏幕。

代码如下:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("com.android.settings", "com.android.settings.Settings$DataUsageSummaryActivity");
startActivity(intent);

【讨论】:

  • 注意:在某些设备上会导致ActivityNotFoundException(我的案例是客户在android7上的小米红米Note)。
  • 在上面的列表中添加了华为 P9 lite。这也是在adnroid7上,所以问题可能来自操作系统而不是手机。
【解决方案3】:

试试这个代码

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

【讨论】:

    【解决方案4】:

    你可以试试这个:

    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();
        }
    

    希望对你有帮助!!!发现于https://github.com/kostasdrakonakis/dialog/blob/master/Custom%20Dialog/DialogMessageDisplay.java

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-04
      • 1970-01-01
      • 1970-01-01
      • 2011-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-02
      相关资源
      最近更新 更多