android 如何通过app来关机,或者捕获home键
答案:2 悬赏:10 手机版
解决时间 2021-02-13 12:26
- 提问者网友:兔牙战士
- 2021-02-12 21:07
我主要是想做一款定时/倒计时关机的软件,可是发现android4+的系统没有开放api,求指导,通过追加财富值
最佳答案
- 五星知识达人网友:雾月
- 2021-02-12 21:37
app 关机
ProcessBuilder 获得linux权限后,执行关机命令
home建 使用广播监听
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
String reason = intent.getStringExtra(SYSTEM_REASON);
if (TextUtils.equals(reason, SYSTEM_HOME_KEY)) {
//表示按了home键,程序到了后台
Toast.makeText(getApplicationContext(), "home", 1).show();
}else if(TextUtils.equals(reason, SYSTEM_HOME_KEY_LONG)){
//表示长按home键,显示最近使用的程序列表
}
}
}
附上封装类
package com.example.linuxcmd;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public final class RootCmd {
// 执行linux命令并且输出结果
protected static String exec(String[] args) {
String result = "";
ProcessBuilder processBuilder = new ProcessBuilder(args);
Process process = null;
InputStream errIs = null;
InputStream inIs = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int read = -1;
process = processBuilder.start();
errIs = process.getErrorStream();
while ((read = errIs.read()) != -1) {
baos.write(read);
}
baos.write('\n');
inIs = process.getInputStream();
while ((read = inIs.read()) != -1) {
baos.write(read);
}
byte[] data = baos.toByteArray();
result = new String(data);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (errIs != null) {
errIs.close();
}
if (inIs != null) {
inIs.close();
}
} catch (IOException e) {
e.printStackTrace();
}
if (process != null) {
process.destroy();
}
}
return result;
}
}
ProcessBuilder 获得linux权限后,执行关机命令
home建 使用广播监听
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
String reason = intent.getStringExtra(SYSTEM_REASON);
if (TextUtils.equals(reason, SYSTEM_HOME_KEY)) {
//表示按了home键,程序到了后台
Toast.makeText(getApplicationContext(), "home", 1).show();
}else if(TextUtils.equals(reason, SYSTEM_HOME_KEY_LONG)){
//表示长按home键,显示最近使用的程序列表
}
}
}
附上封装类
package com.example.linuxcmd;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public final class RootCmd {
// 执行linux命令并且输出结果
protected static String exec(String[] args) {
String result = "";
ProcessBuilder processBuilder = new ProcessBuilder(args);
Process process = null;
InputStream errIs = null;
InputStream inIs = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int read = -1;
process = processBuilder.start();
errIs = process.getErrorStream();
while ((read = errIs.read()) != -1) {
baos.write(read);
}
baos.write('\n');
inIs = process.getInputStream();
while ((read = inIs.read()) != -1) {
baos.write(read);
}
byte[] data = baos.toByteArray();
result = new String(data);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (errIs != null) {
errIs.close();
}
if (inIs != null) {
inIs.close();
}
} catch (IOException e) {
e.printStackTrace();
}
if (process != null) {
process.destroy();
}
}
return result;
}
}
全部回答
- 1楼网友:duile
- 2021-02-12 21:51
你指的不是关机吧?是黑屏吧
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯