最近在为公司做定制Launcher,其中要实现锁屏、重启和关机功能,开始捣鼓了好久,还去倒腾了倒腾android源码… … 最后,终于找到一个不用跑底层的方法,简单实用,霸道直接!!!
话不多说,直接上代码,最后是非常重要的注意事项!!!
—————————————–start
一:锁屏
step1:button时间直接响应
private DevicePolicyManager policyManager;
private ComponentName componentName;
//一键锁屏
public void lockWindow(View view) {
policyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
componentName = new ComponentName(this, LockReceiver.class);
if (policyManager.isAdminActive(componentName)) {
policyManager.lockNow();
android.os.Process.killProcess(android.os.Process.myPid());
} else {
activeManager();
}
}
step2:所需类和方法
package com.god.power;
import android.app.admin.DeviceAdminReceiver;
import android.content.Context;
import android.content.Intent;
/**
* @author David create on 2016/10/19 10:58.
* @email david.forever.god@gmail.com
* Learn from yesterday, live for today, hope for tomorrow.
*/
public class LockReceiver extends DeviceAdminReceiver {
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
}
@Override
public void onEnabled(Context context, Intent intent) {
//激活使用
super.onEnabled(context, intent);
}
@Override
public void onDisabled(Context context, Intent intent) {
//取消激活
super.onDisabled(context, intent);
}
}
别忘了在清单文件中注册广播
<!-- 注册锁屏广播 -->
<receiver
android:name=".LockReceiver"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/lock_screen" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
resource里的XML文件(真不知道有没有用)
<?xml version="1.0" encoding="utf-8"?>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android" >
<uses-policies>
<!-- 锁定屏幕 -->
<force-lock />
</uses-policies>
</device-admin>
还有activeManager
private void activeManager() {
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "一键锁屏");
startActivity(intent);
}
还有在onResume里
@Override
protected void onResume() {
super.onResume();
/**
* @author David created at 2016/11/9 11:59
* 当你第一次激活锁屏功能后实现锁屏
*/
if (policyManager != null && policyManager.isAdminActive(componentName)) {
policyManager.lockNow();
android.os.Process.killProcess(android.os.Process.myPid());
}
}
至此,用系统签名打包安装即可。
二:重启和关机
重启
PowerManager pManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
pManager.reboot("重启");
关机
try {
Class<?> ServiceManager = Class
.forName("android.os.ServiceManager");
Method getService = ServiceManager.getMethod("getService", java.lang.String.class);
Object oRemoteService = getService.invoke(null, Context.POWER_SERVICE);
Class<?> cStub = Class
.forName("android.os.IPowerManager$Stub");
Method asInterface = cStub.getMethod("asInterface", android.os.IBinder.class);
Object oIPowerManager = asInterface.invoke(null, oRemoteService);
Method shutdown = oIPowerManager.getClass().getMethod("shutdown", boolean.class, boolean.class);
shutdown.invoke(oIPowerManager, false, true);
} catch (Exception e) {
Log.e("David", e.toString(), e);
}
你不需要看懂,你只需要复制就行,能看懂这个的,也不会来看我的博客… …
下面是我项目中用到了Dialog提示框,一起粘出来
//一键重启
public void reboot(View view) {
mReceiverWindow("重启");
}
//一键关机
public void shutdown(View view) {
mReceiverWindow("关机");
}
private void mReceiverWindow(final String str) {
if (str.equals("重启")) {
dialogHint = "重启";
} else {
dialogHint = "关机";
}
new AlertDialog.Builder(this)
.setTitle("提示")
.setMessage(dialogHint)
.setPositiveButton(str, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (str.equals("重启")) {
Message obtain = Message.obtain();
obtain.what = 100;
mPowerHandler.sendMessage(obtain);
}
if (str.equals("关机")) {
Message obtain = Message.obtain();
obtain.what = 200;
mPowerHandler.sendMessage(obtain);
}
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).show();
}
/**
* @author David created at 2016/11/9 11:52
* 为什么要用Handler呢?
* 因为Dialog点击确定后消失是需要时间去执行的,如果不用Handler的话,点击了重启或者关机按钮后,
* 系统将会立刻执行重启或关机操作,这时候,提示重启或关机的Dialog框是不会消失掉的,就会造成提示Dialog
* 框和正在关机的提示框重叠现象,视觉效果极差。当然你不用Dialog提示框就没有这个问题了。
*/
private Handler mPowerHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 100://重启实现
PowerManager pManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
pManager.reboot("重启");
break;
case 200://关机实现
try {
Class<?> ServiceManager = Class
.forName("android.os.ServiceManager");
Method getService = ServiceManager.getMethod("getService", java.lang.String.class);
Object oRemoteService = getService.invoke(null, Context.POWER_SERVICE);
Class<?> cStub = Class
.forName("android.os.IPowerManager$Stub");
Method asInterface = cStub.getMethod("asInterface", android.os.IBinder.class);
Object oIPowerManager = asInterface.invoke(null, oRemoteService);
Method shutdown = oIPowerManager.getClass().getMethod("shutdown", boolean.class, boolean.class);
shutdown.invoke(oIPowerManager, false, true);
} catch (Exception e) {
Log.e("David", e.toString(), e);
}
break;
}
}
};
最后清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.god.power"
android:sharedUserId="android.uid.system"><!-- 系统权限 **一定不要忘记** -->
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="15" />
<!-- 权限 -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.SHUTDOWN"
tools:ignore="ProtectedPermissions" />
<uses-permission
android:name="android.permission.REBOOT"
tools:ignore="ProtectedPermissions" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 注册锁屏广播 -->
<receiver
android:name=".LockReceiver"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/lock_screen" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
</application>
</manifest>
重点注意事项
这时候,你如果点run,将应用跑到机器上,你会发现会失败的!所以,你需要打包之后再安装上才能正常使用,然而打包的时候,特别注意,一定要用系统签名来打包,至于系统签名,我用的公司自己的rom,自己的系统签名,如果你没有,推荐一篇博文http://blog.csdn.net/jingwen3699/article/details/8024900
这样就能在指定的rom里实现这三个功能了。
最后,至于效果图,就没必要展示了,谢谢
—————————————– end
========================
Remember Me
@ Name : David
@ email :david.forever.god@gmail.com
Learn from yesterday, live for today, hope for tomorrow.
Thanks for you!!!
Have a nice day !!!