黑马程序员技术交流社区
标题:
ANDROID一键锁屏
[打印本页]
作者:
woshiku
时间:
2015-9-8 14:46
标题:
ANDROID一键锁屏
package com.onekeylock.activity;
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow;
import com.onekeylock.R;
import com.onekeylock.receive.AdminReceiver;
public class Main extends Activity implements OnClickListener {
private Button openDevice;
private Button lock;
private Button wipeData;
private DevicePolicyManager mDPM;
private ComponentName mDeviceAdminSample;
private PopupWindow waitWindow =null;
private void initWaitWindow(){
if(waitWindow == null){
View view = View.inflate(Main.this, R.layout.popup_wait, null);
waitWindow = new PopupWindow(view, -2, -2);
waitWindow.showAtLocation(findViewById(R.id.theone), Gravity.CENTER, 0, 0);
}
}
private void initViews(){
openDevice = (Button)findViewById(R.id.open_device);
lock = (Button)findViewById(R.id.lockscreen);
wipeData = (Button)findViewById(R.id.wipe_data);
openDevice.setOnClickListener(this);
lock.setOnClickListener(this);
wipeData.setOnClickListener(this);
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
}
// 激活设备管理器, 也可以在设置->安全->设备管理器中手动激活
public void activeAdmin() {
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
mDeviceAdminSample);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
"哈哈哈, 我们有了超级设备管理器, 好NB!");
startActivity(intent);
}
// 一键锁屏
public void lockScreen() {
if (mDPM.isAdminActive(mDeviceAdminSample)) {// 判断设备管理器是否已经激活
mDPM.lockNow();// 立即锁屏
mDPM.resetPassword("1234", 0);
} else {
activeAdmin();
lockScreen();
//Toast.makeText(this, "必须先激活设备管理器!", Toast.LENGTH_SHORT).show();
}
}
public void clearData() {
if (mDPM.isAdminActive(mDeviceAdminSample)) {// 判断设备管理器是否已经激活
mDPM.wipeData(0);// 清除数据,恢复出厂设置
} else {
//Toast.makeText(this, "必须先激活设备管理器!", Toast.LENGTH_SHORT).show();
}
}
public void unInstall() {
mDPM.removeActiveAdmin(mDeviceAdminSample);// 取消激活
// 卸载程序
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initViews();
mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);// 获取设备策略服务
mDeviceAdminSample = new ComponentName(this, AdminReceiver.class);// 设备管理组件
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.open_device:
//Toast.makeText(Main.this, "打开", Toast.LENGTH_SHORT).show();
activeAdmin();
break;
case R.id.lockscreen:
lockScreen();
break;
case R.id.wipe_data:
unInstall() ;
break;
}
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/theone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Main" >
<Button
android:id="@+id/open_device"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp"
android:text="打开设备管理器" />
<Button
android:id="@+id/lockscreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/open_device"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp"
android:text="一键锁屏" />
<Button
android:id="@+id/wipe_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/lockscreen"
android:layout_centerHorizontal="true"
android:layout_marginTop="59dp"
android:text="卸载" />
</RelativeLayout>
有需要的同学可以参考一下
一键锁屏.rar
2015-9-8 14:45 上传
点击文件名下载附件
1.38 MB, 下载次数: 160
源码加APP
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2