logo头像

勤求古训,博采众方

Android系统级应用常用API

本文于 904 天之前发表,文中内容可能已经过时。

Android系统级应用开发中常用的方法,工具类汇总

常见API

同步系统时间

  • 1.我们的应用需要拥有系统签名才能修改系统时间,我们找设备供应商给自己的应用加上了系统签名。

  • 2.需要在Manifest添加如下两个权限

1
2
3
4
<uses-permission
android:name="android.permission.SET_TIME_ZONE" />
<uses-permission
android:name="android.permission.SET_TIME"/>
  • 3.我们获取通过接口获取服务器的时间戳,根据服务器的时间戳来修改安卓设备的时间,上代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public static void doSetLocalTime(Context mContext, long time) {
boolean is24Hour = DateFormat.is24HourFormat(mContext);
if (!is24Hour) {
android.provider.Settings.System.putString(mContext.getContentResolver(),
android.provider.Settings.System.TIME_12_24, "24");
}
try {
boolean isAUTO_TIME_ZONE = android.provider.Settings.Global.getInt(mContext.getContentResolver(),
android.provider.Settings.Global.AUTO_TIME_ZONE) > 0;
if (isAUTO_TIME_ZONE) {
android.provider.Settings.Global.putInt(mContext.getContentResolver(),
android.provider.Settings.Global.AUTO_TIME_ZONE, 0);
}
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
try {
boolean AUTO_TIME = android.provider.Settings.Global.getInt(mContext.getContentResolver(),
android.provider.Settings.Global.AUTO_TIME) > 0;
if (AUTO_TIME) {
android.provider.Settings.Global.putInt(mContext.getContentResolver(),
android.provider.Settings.Global.AUTO_TIME, 0);
}
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
if (time / 1000 < Integer.MAX_VALUE) {
((AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE)).setTimeZone("GMT+08:00");
((AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE)).setTime(time);
}
}

系统重启

  • 由于 App 有系统签名,所以实现系统重启功能比较简单。其中的一种方式是使用 PowerManager 实现。代码如下:
1
2
3
4
// 重新启动到 fastboot模式
PowerManager pManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
pManager.reboot("");

禁止下拉设置按钮

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* 用反射实现控制StatusBar状态,需要是system应用
*/
public class StatusBarUtil {
public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
public static final int STATUS_BAR_DISABLE_NONE = 0x00000000;

public static void setStatusBarState(Context context, int disableFlag) {
@SuppressLint("WrongConstant") Object service = context.getSystemService("statusbar");
try {
Class<?> statusbarManager = Class.forName("android.app.StatusBarManager");
Method disable = statusbarManager.getMethod("disable", int.class);
disable.invoke(service, disableFlag);
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 禁用顶部状态栏的下拉及底部虚拟的回退等按键
*/
public static void disableStatusBar(Context context) {
// Disable *all* possible navigation via the system bar.
int state = STATUS_BAR_DISABLE_EXPAND ;
setStatusBarState(context, state);
}

public static void enableStatusBar(Context context) {
setStatusBarState(context, STATUS_BAR_DISABLE_NONE);
}

}

参考

Android App 设置系统时间,语言和时区、系统重启
Android 静默安装并自动重启