Android编程程序实现一键锁屏的方法讲解

所属分类: 软件编程 / Android 阅读数: 114
收藏 0 赞 0 分享

Android程序之一键锁屏

(1)布局文件activity_main.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MainActivity" >
  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="openAdmin"
    android:text="开启管理员权限" />
  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:onClick="lockscreen"
    android:text="一键锁屏" />
  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:onClick="uninstall"
    android:text="卸载软件" />
</RelativeLayout>

(2)MainActivity.java

package com.xuliugen.lockscreen;
import com.itheima.lockscreen.R;
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
  /**
   * 设备策略服务
   */
  private DevicePolicyManager dpm;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
  }
  /**
   * 用代码去开启管理员
   */
  public void openAdmin(View view) {
    // 创建一个Intent
    Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
    // 我要激活谁
    ComponentName mDeviceAdminSample = new ComponentName(this,MyAdmin.class);
    intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,mDeviceAdminSample);
    // 劝说用户开启管理员权限
    intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,"哥们开启我可以一键锁屏,你的按钮就不会经常失灵");
    startActivity(intent);
  }
  /**
   * 一键锁屏
   */
  public void lockscreen(View view) {
    ComponentName who = new ComponentName(this, MyAdmin.class);
    if (dpm.isAdminActive(who)) {
      dpm.lockNow();// 锁屏
      dpm.resetPassword("", 0);// 设置屏蔽密码
      // 清除Sdcard上的数据
      // dpm.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE);
      // 恢复出厂设置
      // dpm.wipeData(0);
    } else {
      Toast.makeText(this, "还没有打开管理员权限", 1).show();
      return;
    }
  }
  /**
   * 卸载当前软件
   */
  public void uninstall(View view) {
    // 1.先清除管理员权限
    ComponentName mDeviceAdminSample = new ComponentName(this,
        MyAdmin.class);
    dpm.removeActiveAdmin(mDeviceAdminSample);
    // 2.普通应用的卸载
    Intent intent = new Intent();
    intent.setAction("android.intent.action.VIEW");
    intent.addCategory("android.intent.category.DEFAULT");
    intent.setData(Uri.parse("package:" + getPackageName()));
    startActivity(intent);
  }
}

(3)根据API文档可知,需要一个类继承DeviceAdminReceiver:

package com.xuliugen.lockscreen;
import android.app.admin.DeviceAdminReceiver;
/**
 * 特殊的广播接收者
 * @author xuliugen
 */
public class MyAdmin extends DeviceAdminReceiver {
}

(4)广播接受者的设置(清单文件):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.xuliugen.lockscreen"
  android:versionCode="1"
  android:versionName="1.0" >
  <uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="16" />
  <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
      android:name="com.xuliugen.lockscreen.MainActivity"
      android:label="@string/app_name" >
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    <!-- 广播接收者 -->
    <receiver
      android:name="com.xuliugen.lockscreen.MyAdmin"
      android:description="@string/sample_device_admin_description"
      android:label="@string/sample_device_admin"
      android:permission="android.permission.BIND_DEVICE_ADMIN" >
      <meta-data
        android:name="android.app.device_admin"
        android:resource="@xml/device_admin_sample" />
      <intent-filter>
        <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
      </intent-filter>
    </receiver>
  </application>
</manifest>

运行效果:

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多