发布时间:2026-09-28 18:17:52 浏览次数:5
在 Android 应用中实现系统重新启动的功能,主要可以通过两种方式实现:
ACTION_REBOOT 广播实现系统重新启动。su 的命令执行 /system/bin/reboot 实现系统重新启动。以下是具体操作步骤和示例说明。
ACTION_REBOOT 广播实现系统重新启动在 Android 应用中发送 ACTION_REBOOT 广播,即可实现系统重新启动的功能,以下是具体步骤:
在 androidManifest.xml 中添加以下权限:
<uses-permission android:name="android.permission.REBOOT" />在代码中发送 ACTION_REBOOT 广播,即可实现系统重新启动的功能:
Intent intent = new Intent(Intent.ACTION_REBOOT);intent.putExtra("nowait", 1);intent.putExtra("interval", 1);intent.putExtra("window", 0);sendBroadcast(intent);其中,nowait 表示立即重启,interval 表示重启前等待时间,单位为秒,window 表示界面选项,0 表示不显示重启对话框。
下面是一个示例,该示例点击按钮后即可实现系统重新启动的功能:
public class MainActivity extends AppCompatActivity { private Button mRebootBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRebootBtn = findViewById(R.id.reboot_btn); mRebootBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { rebootSystem(); } }); } private void rebootSystem() { Intent intent = new Intent(Intent.ACTION_REBOOT); intent.putExtra("nowait", 1); intent.putExtra("interval", 1); intent.putExtra("window", 0); sendBroadcast(intent); }}su 的命令执行 /system/bin/reboot 实现系统重新启动在 Android 应用中通过 su 命令执行 /system/bin/reboot 命令,即可实现系统重新启动的功能,以下是具体步骤:
在 androidManifest.xml 中添加以下权限:
<uses-permission android:name="android.permission.ACCESS_SUPERUSER" />在代码中通过 su 命令执行 /system/bin/reboot 命令,即可实现系统重新启动的功能:
Process process = null;try { process = Runtime.getRuntime().exec("su"); DataOutputStream outputStream = new DataOutputStream(process.getOutputStream()); outputStream.writeBytes("/system/bin/reboot\n"); outputStream.flush(); outputStream.writeBytes("exit\n"); outputStream.flush();} catch (IOException e) { e.printStackTrace();}下面是一个示例,该示例点击按钮后即可实现系统重新启动的功能:
public class MainActivity extends AppCompatActivity { private Button mRebootBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRebootBtn = findViewById(R.id.reboot_btn); mRebootBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { rebootSystem(); } }); } private void rebootSystem() { Process process = null; try { process = Runtime.getRuntime().exec("su"); DataOutputStream outputStream = new DataOutputStream(process.getOutputStream()); outputStream.writeBytes("/system/bin/reboot\n"); outputStream.flush(); outputStream.writeBytes("exit\n"); outputStream.flush(); } catch (IOException e) { e.printStackTrace(); } }}