輕鬆實現Android鎖屏功能_Android

來源:互聯網
上載者:User

鎖屏需要引入裝置超級管理員。在文檔Android開發文檔的Administration中有詳細的說明。Android裝置管理系統功能和控制訪問。

主要有一下幾個步驟:

1  建立廣播接收者,實現DeviceAdminReceiver

package com.andy.lockscreen;  import android.app.admin.DeviceAdminReceiver;  /**  * @author Zhang,Tianyou  * @version 2014年11月20日 下午9:51:42  *  * 特殊的廣播接受者 接收 管理員權限廣播  */  public class MyAdmin extends DeviceAdminReceiver{  } 

2 在資訊清單檔中註冊該廣播(不同普通的廣播,需按照說明格式):

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"   package="com.andy.lockscreen"   android:versionCode="1"   android:versionName="1.0" >    <uses-sdk     android:minSdkVersion="8"     android:targetSdkVersion="17" />    <application     android:allowBackup="true"     android:icon="@drawable/ic_launcher"     android:label="@string/app_name"     android:theme="@style/AppTheme" >     <activity       android:name=".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=".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> 

3 在res下建立xml檔案夾,建立對應的xml檔案device_admin_sample.xml

<device-admin xmlns:android="http://schemas.android.com/apk/res/android">  <uses-policies>   <limit-password />   <watch-login />   <reset-password />   <force-lock />   <wipe-data />   <expire-password />   <encrypted-storage />   <disable-camera />  </uses-policies> </device-admin> 

4 在values檔案下string.xml添加

<string name="sample_device_admin_description">使用者管理員的描述資訊</string> <string name="sample_device_admin">設定系統管理權限</string> 

5 介面檔案:

<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"   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="com.andy.lockscreen.MainActivity" >     <Button     android:onClick="openAdmin"     android:layout_alignParentTop="true"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="開啟管理員權限" />      <Button     android:onClick="lockcreen"     android:layout_centerInParent="true"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="一鍵鎖屏" />    <Button     android:onClick="uninstall"     android:layout_alignParentBottom="true"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="卸載鎖屏" /> </RelativeLayout> 

6 實現鎖屏和開啟裝置管理員許可權,卸載檔案

package com.andy.lockscreen;  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) {     // TODO Auto-generated method stub     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_main);     dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);   }    /**    * 鎖屏    *    * @param view    */   public void lockcreen(View view) {     ComponentName who = new ComponentName(this, MyAdmin.class);     // 判斷是否已經開啟管理員權限     if (dpm.isAdminActive(who)) {       // 鎖屏       dpm.lockNow();       // 設定螢幕密碼 第一個是密碼 第二個是附加參數       dpm.resetPassword("123", 0);        // 清楚資料       // WIPE_EXTERNAL_STORAGE 清楚sdcard的資料       // 0 恢復出廠預設值       //dpm.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE);     } else {       // 如果為未開啟 提示       Toast.makeText(MainActivity.this, "請先開啟管理員權限!", Toast.LENGTH_SHORT)           .show();     }   }       /**    * 代碼開啟系統管理權限    *    * @param view    */   public void openAdmin(View view) {     // 建立一個Intent 添加裝置管理員     Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);     // 啟用MyAdmin廣播接收著     ComponentName who = new ComponentName(this, MyAdmin.class);      intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, who);     // 說明使用者開啟管理員權限的好處     intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,         "開啟可以一鍵鎖屏,防止勿碰");     startActivity(intent);          Toast.makeText(MainActivity.this, "管理員權限已開啟!", Toast.LENGTH_SHORT).show();   }    /**    * 卸載當前的軟體 裝置管理資料特殊應用 所以不能普通卸載    */    public void uninstall(View view) {     // 1. 先清除管理員權限     ComponentName who = new ComponentName(this,         MyAdmin.class);     dpm.removeActiveAdmin(who);      // 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);    }  } 

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.