在Setting中增加一项用来执行shell脚本,将脚本打印信息显示在Alertdialog中。
记录如何在Setting中增加一项,点击执行shell脚本,将脚本打印信息显示在Alertdialog中。一.在Setting中增加一项1.增加一个PreferenceActivity.packages/apps/Settings/src/com/android/settings/DataBackupRestore.java2.增加布局packages/apps/Setting
记录如何在Setting中增加一项,点击执行shell脚本,将脚本打印信息显示在Alertdialog中。脚本执行中显示sbr等待,脚本执行后显示脚本输出结果。
一.在Setting中增加一项
1.增加一个PreferenceActivity.
packages/apps/Settings/src/com/android/settings/DataBackupRestore.java
2.增加布局
packages/apps/Settings/res/xml/data_data_restore.xml
3.注册这个PreferenceActivity.
packages/apps/Settings/AndroidManifest.xml
4.将这个PreferenceActivity添加到Setting.
packages/apps/Settings/res/xml/device_info_settings.xml(根据位置不同这个xml文件可能不同)
PS:这几个步骤都不难,不具体说明。
二,实现执行shell脚本,将脚本打印信息显示在Alertdialog中。
1.其实这些都是在 packages/apps/Settings/src/com/android/settings/DataBackupRestore.java 中实现的:
public class DataBackupRestore extends PreferenceActivity {
private static final String TAG = "DataBackupRestore";
private Preference backup_preference ;
private Preference restore_preference ;
private AlertDialog.Builder builder;
private View view;
public TextView Text1 ;
private ProgressBar bar;
private Button bt1;
Handler handler = new Handler(){ //增加 Handler用来修改AlertDialog UI
@Override
public void handleMessage(Message msg){
int what = msg.what;
if(what == 0){
Text1.setText(msg.obj.toString());
Text1.setVisibility(View.VISIBLE);
bar.setVisibility(View.GONE);
}
}
} ;
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
addPreferencesFromResource(R.xml.data_data_restore);
backup_preference = (Preference) findPreference("user_data_backup");
restore_preference = (Preference) findPreference("user_data_restore");
builder =new AlertDialog.Builder(this);
view=(LinearLayout) getLayoutInflater().inflate(R.layout.backup_restore_status,null); //自定义AlertDialog,增加自己的布局
Text1 = (TextView)view.findViewById(R.id.inputNum); //找到自定义布局的元素
bar = (ProgressBar) view.findViewById(R.id.progressBar1);
bt1=(Button) view.findViewById(R.id.button110);
}
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
if(preference == backup_preference){
builder.setMessage("正在备份...");
builder.setTitle("系统提示");
builder.setView(view);
builder.setPositiveButton("退出", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.create().show(); //显示AlertDialog
new Thread(new Runnable(){ //线程执行shell脚本
@Override
public void run() {
// TODO Auto-generated method stub
try
{
Process localProcess = Runtime.getRuntime().exec("su");
OutputStream localOutputStream = localProcess.getOutputStream();
DataOutputStream localDataOutputStream = new DataOutputStream(localOutputStream);
localDataOutputStream.writeBytes("/system/bin/backUP_restore.sh backup 2>/dev/null\n");
// localProcess.waitFor();
InputStream localInputStream = localProcess.getInputStream();
BufferedReader read = new BufferedReader(new InputStreamReader(localInputStream));
String result = read.readLine();
Message m = handler.obtainMessage(0, 1, 1, result); //构造要传递的消息
handler.sendMessage(m); //传递消息到Handler,让其改变UI。
}
catch(IOException e)
{
e.printStackTrace();
}
}
}).start();
}
return true;
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:orientation="vertical"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent"
6 android:gravity="center_horizontal">
7
8 <TextView
9 android:layout_width="wrap_content"
10 android:layout_height="wrap_content"
11 android:id="@+id/Text1"
12 android:textSize="20sp"
13 android:visibility="gone"/>
14
15 <ProgressBar
16 android:id="@+id/progressBar1"
17 android:layout_width="wrap_content"
18 android:layout_height="wrap_content" />
19
20 </LinearLayout>
更多推荐
所有评论(0)