java - 在 Android API 17 中打开/关闭移动数据

标签 java android-4.2-jelly-bean

API级别17中使用android adb shell,我可以使用svc数据启用/禁用启用和禁用移动数据,但如果我尝试务实它确实有效并且通过,没有任何异常,这是我的代码

    try{ Log.e(TAG,"Before mobile data thread");
    Process proc = Runtime.getRuntime().exec("su");
    DataOutputStream os= new DataOutputStream(proc.getOutputStream());
    os.writeBytes("svc data disable\n");
    os.writeBytes("exit\n");
    os.flush();
    try{
        proc.waitFor();
        if(proc.exitValue() !=255)
        {
            Log.e(TAG,"Disabling mobile data");
        }else{
            Log.e(TAG,"Error Disabling mobile data");

            }

        }catch(Exception e){}
} catch (Exception ex){Log.e(TAG,"IN mobile data exception exception");} 

任何建议我如何禁用/启用移动数据

最佳答案

如果手机未 root 或您的应用未处于设备管理员模式,您无法自行启用或禁用移动数据。您只能要求用户启用/禁用应用程序数据。

您可以在对话框中询问用户,如下所示:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Please turn on mobile data access")
    .setTitle("Unable to connect")
    .setCancelable(false)
    .setPositiveButton("Settings", (dialog, id) -> startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS)))
    .setNegativeButton("Cancel", null)
    .show()

对于 root 后的手机使用 this answer

关于java - 在 Android API 17 中打开/关闭移动数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40337601/

相关文章:

java - 程序在Java中抛出java.lang.UnsupportedOperationException

java - Scala+Android+Roboguice : ClassNotFoundException in loader

java - libgdx toJson() 通过 REST API 更新 Parse.com 上的整数字段

android - Jelly Bean 4.2 上的文件目录

android - 操作栏菜单项的分隔线

java - TestLink XML-RPC 对 xml-rpc 客户端的调用失败

android - 如何在果冻 bean 中使用 "Select debug app"和 "wait for debugger"新功能?

android - httpurlconnection 在 Android 4.2 上非常慢

安卓通知 : compatibility with APIs

java - 从 json 中转义 "["括号