android java linux_Android开发:java程序调用linux命令(高级)

0818b9ca8b590ca3270a3433284dd417.png

DefyPrcHelper国行2.3.4ROM小问题修复工具

最近手机升级到了2.3.4(moto大发慈悲);用着还好,但是也有些用户体验不好的地方,比如相机不能静音,(我邪恶了?),键盘灯太亮等等,故此 做 了这个小应用.

主要软件功能:

+消除恢复照相机摄像机声音

+去除创建联系人以外崩溃的Bug(当然如果遇到其他问题,实际是disable "BlurContactsSync.apk";

+直接没有发现引起其他问题,如果用户发现也可以点击恢复按钮恢复)

+开启.关闭键盘灯

感谢Cye3s的脚本

package com.su.defyPrcHelper;

/*

*实现高级的linux命令调要用,本实例处理了一些Android系统内部的文件

*实现在Android程序下调用Shell文件.

*ssfshine@gmail.com

*/

import java.io.BufferedReader;

import java.io.DataOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import com.su.defyPrcHelper.R;

import android.app.Activity;

import android.content.res.AssetManager;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.Toast;

public class defyPrcHelper extends Activity {

private static final String DATA_PATH = "/data/data/com.su.defyPrcHelper/";

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

CopyAssets();//把文件复制到程序目录下

Button btExit = (Button) findViewById(R.id.button6);

btExit.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

finish();

android.os.Process.killProcess(android.os.Process.myPid()); //彻底关闭应用程序

}

});

Button btClose = (Button) findViewById(R.id.button5);

btClose.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

runCommand("chmod 777 " + DATA_PATH + "DefyBacklightSwitch.sh");//是文件可读可写

RootCmd(DATA_PATH + "DefyBacklightSwitch.sh");//运行shell文件

Toast.makeText(

defyPrcHelper.this, "操作成功!",Toast.LENGTH_LONG

).show();

}

});

Button btBC = (Button) findViewById(R.id.button1);

btBC.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

RootCmd("mv /system/app/BlurContactsSync.apk /system/app/BlurContactsSync.apk1");

RootCmd("mv /system/app/BlurContactsSync.odex /system/app/BlurContactsSync.odex1");

Toast.makeText(

defyPrcHelper.this, "消除联系人bug成功!",Toast.LENGTH_LONG

).show();

}

});

Button btCV = (Button) findViewById(R.id.button2);

btCV.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

RootCmd("mv /system/media/audio/ui/camera_click.ogg /system/media/audio/ui/camera_click.ogg1");

RootCmd("mv /system/media/audio/ui/VideoRecord.ogg /system/media/audio/ui/VideoRecord.ogg1");

Toast.makeText(

defyPrcHelper.this, "消除摄像照相声音成功",Toast.LENGTH_LONG

).show();

}

});

Button  btReBC = (Button) findViewById(R.id.button3);

btReBC.setOnClickListener(new View.OnClickListener(){

public void onClick(View v) {

RootCmd("mv /system/app/BlurContactsSync.apk1 /system/app/BlurContactsSync.apk");

RootCmd("mv /system/app/BlurContactsSync.odex1 /system/app/BlurContactsSync.odex");

Toast.makeText(

defyPrcHelper.this, "恢复联系人bug成功",Toast.LENGTH_LONG

).show();

}

});

Button  btReCV= (Button) findViewById(R.id.button4);

btReCV.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

RootCmd("mv /system/media/audio/ui/VideoRecord.ogg1 /system/media/audio/ui/VideoRecord.ogg");

RootCmd("mv /system/media/audio/ui/camera_click.ogg1 /system/media/audio/ui/camera_click.ogg");

Toast.makeText(

defyPrcHelper.this, "恢复声音成功",Toast.LENGTH_LONG

).show();

}

});

}

public static boolean RootCmd(String cmd){//运行需要root权限的命令

Process process = null;

DataOutputStream os = null;

try{

process = Runtime.getRuntime().exec("su");

os = new DataOutputStream(process.getOutputStream());

os.writeBytes(cmd + "\n");

os.writeBytes("exit\n");

os.flush();

process.waitFor();

} catch (Exception e) {

return false;

}

finally {

try {

if (os != null)   {

os.close();

}

process.destroy();

}

catch (Exception e) {

}

}

return true;

}

private void CopyAssets() {

AssetManager assetManager = getAssets();

String[] files = null;

try {

files = assetManager.list("");

} catch (IOException e) {

}

for (int i = 0; i < files.length; i++) {

InputStream in = null;

OutputStream out = null;

try {

if (!(new File(DATA_PATH + files[i])).exists()) {

in = assetManager.open(files[i]);

out = new FileOutputStream(DATA_PATH + files[i]);

copyFile(in, out);

in.close();

in = null;

out.flush();

out.close();

out = null;

}

} catch (Exception e) {

}

}

}

private void copyFile(InputStream in, OutputStream out) throws IOException {

byte[] buffer = new byte[1024];

int read;

while ((read = in.read(buffer)) != -1) {

out.write(buffer, 0, read);

}

}

public static boolean runCommand(String command) {//运行普通linux命令

Process process = null;

try {

process = Runtime.getRuntime().exec(command);

process.waitFor();

} catch (Exception e) {

return false;

} finally {

try {

process.destroy();

} catch (Exception e) {

// nothing

}

}

return true;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值