2013年10月28日 星期一

turn on airplane mode (root shell command)

package com.example.testallstar;

import java.io.DataOutputStream;
import java.io.IOException;

import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.app.Activity;
import android.content.Intent;
import android.telephony.ServiceState;
import android.view.Menu;

public class MainActivity extends Activity {

String[] commands = {"sysrw", "settings put global airplane_mode_on 1\nam broadcast -a android.intent.action.AIRPLANE_MODE --ez state true", "sysro"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RunAsRoot(commands);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

public void RunAsRoot(String[] cmds) {
Process p = null;
try {
p = Runtime.getRuntime().exec("su");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DataOutputStream os = new DataOutputStream(p.getOutputStream());
for (String tmpCmd : cmds) {
try {
os.writeBytes(tmpCmd + "\n");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
os.writeBytes("exit\n");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
os.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

//------------------------------------------------------------------------------------

4down voteaccepted
There is a new "settings" binary in Android 4.2 You can also use it without su, then your app needs the permission required to change this setting declared in your app's manifest (which would be WRITE_SECURE_SETTINGS for flight mode in 4.2 - which is only granted to apps installed on system partition).
Activate
su 
settings put global airplane_mode_on 1
am broadcast -a android.intent.action.AIRPLANE_MODE --ez state true
Deactivate
su
settings put global airplane_mode_on 0
am broadcast -a android.intent.action.AIRPLANE_MODE --ez state false
For other android versions and other settings (flight mode can be activated without root before 4.2) you can use sql injects in settings.db
su
sqlite3 /data/data/com.android.providers.settings/databases/settings.db
insert into global values(null, 'airplane_mode_on', 1);
Replace "global" with "system" or "secure" and 'airplane_mode_on' with the key of your desired table entry. For some settings you need to send certain broadcasts afterwards, see example above for flight mode.
To explore your settings.db run this in a terminal app:
su
sqlite3 /data/data/com.android.providers.settings/databases/settings.db
.tables
select * from global
select * from secure
select * from system
share|improve this answer

沒有留言:

張貼留言

Related Posts Plugin for WordPress, Blogger...