AS写一个APK可以通过按钮查询和修改某个系统文件的文件内容完整代码
时间: 2024-04-30 07:21:31 浏览: 21
以下是一个简单的示例代码,可以通过按钮查询和修改 `/system/build.prop` 文件的内容:
MainActivity.java:
```java
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private EditText editText;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editText);
textView = findViewById(R.id.textView);
Button readButton = findViewById(R.id.readButton);
readButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String content = readSystemFile();
textView.setText(content);
}
});
Button writeButton = findViewById(R.id.writeButton);
writeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String content = editText.getText().toString();
boolean success = writeSystemFile(content);
if (success) {
textView.setText("写入成功");
} else {
textView.setText("写入失败");
}
}
});
}
private String readSystemFile() {
String filePath = "/system/build.prop";
StringBuilder stringBuilder = new StringBuilder();
try {
FileReader fileReader = new FileReader(filePath);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line).append("\n");
}
bufferedReader.close();
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
return stringBuilder.toString();
}
private boolean writeSystemFile(String content) {
String filePath = "/system/build.prop";
try {
FileWriter fileWriter = new FileWriter(filePath, false);
fileWriter.write(content);
fileWriter.flush();
fileWriter.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
```
activity_main.xml:
```xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入要写入的内容" />
<Button
android:id="@+id/writeButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="写入文件" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="文件内容将在此显示" />
<Button
android:id="@+id/readButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="读取文件" />
</LinearLayout>
```
请注意,对 `/system` 目录进行写操作需要 root 权限。此外,修改系统文件可能会导致系统不稳定或不可用,请谨慎操作。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)