android - Android飞行模式更改
问题描述
我有一部生根手机(5.0.1)
我想每 40 秒打开和关闭飞行模式
不工作
Settings.System.putInt(getContentResolver(),Settings.System.AIRPLANE_MODE_ON, 1);
然后我尝试了:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String r1 = run_cmd("adb shell settings put global airplane_mode_on 1");
String r3 = run_cmd("adb shell am broadcast -a android.intent.action.AIRPLANE_MODE");
Log.e("test1", r1);
Log.e("test3", r3);
}
private String run_cmd(String command) {
StringBuilder output = new StringBuilder();
java.lang.Process p;
try {
p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
output.append(line).append("\n");
}
} catch (Exception e) {
e.printStackTrace();
}
return output.toString();
}
}
飞行模式如何改变?
看起来您的帖子主要是代码;请添加更多细节。
解决方案
命令代码
public static final boolean execute(String cmd) {
try {
if (cmd != null && cmd.length() > 0) {
Process p = Runtime.getRuntime().exec("su");
DataOutputStream dos = new DataOutputStream(p.getOutputStream());
dos.writeBytes(cmd + "\n");
dos.writeBytes("exit\n");
dos.flush();
dos.close();
p.waitFor();
} else {
Log.e(TAG, "command is null or empty");
}
} catch (IOException ex) {
Log.e(TAG, "IOException");
ex.printStackTrace();
} catch (SecurityException ex) {
Log.e(TAG, "SecurityException");
ex.printStackTrace();
} catch (Exception ex) {
Log.e(TAG, "Generic Exception");
ex.printStackTrace();
}
return false;
}
主要的
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int second = 50;
new CountDownTimer(second * 1000, second * 1000) {
public void onTick(long l) {
}
public void onFinish() {
RootPrivileges.execute("settings put global airplane_mode_on 1;" +
"am broadcast -a android.intent.action.AIRPLANE_MODE --ez state true;");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
RootPrivileges.execute("settings put global airplane_mode_on 0;" +
"am broadcast -a android.intent.action.AIRPLANE_MODE --ez state false;");
start();
}
}.start();
}
}
简单的代码 - 已解决
推荐阅读
- javascript - 无法删除文件,因为它正被另一个进程 ASP.NET Core MVC 使用
- python - 运行可安装的 Flask 项目错误:无法导入“”
- javascript - 如何使用 Object.assign() 来创建我想要的状态对象
- elasticsearch - 弹性搜索:来自某个地方的匹配字段
- java - 使用数字数组来调整特定返回值的机会?
- android - 无法解析底部导航视图
- python - 如何修复 Python 中的“ImportError:动态模块未定义模块导出函数 (PyInit_cv2)”错误?
- javascript - 如何使每次单击(单个按钮)都会更改标题的内容?
- javascript - 我将如何解决这个曼哈顿天际线/石墙的方法,我哪里出错了?Javascript
- merge - 从单个或多个大文件中排序和删除重复项