欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android編程程序?qū)崿F(xiàn)一鍵鎖屏的方法講解

 更新時(shí)間:2019年03月27日 15:54:10   作者:徐劉根  
今天小編就為大家分享一篇關(guān)于Android編程程序?qū)崿F(xiàn)一鍵鎖屏的方法講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧

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="開啟管理員權(quán)限" />
  <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 {
  /**
   * 設(shè)備策略服務(wù)
   */
  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) {
    // 創(chuàng)建一個(gè)Intent
    Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
    // 我要激活誰
    ComponentName mDeviceAdminSample = new ComponentName(this,MyAdmin.class);
    intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,mDeviceAdminSample);
    // 勸說用戶開啟管理員權(quán)限
    intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,"哥們開啟我可以一鍵鎖屏,你的按鈕就不會(huì)經(jīng)常失靈");
    startActivity(intent);
  }
  /**
   * 一鍵鎖屏
   */
  public void lockscreen(View view) {
    ComponentName who = new ComponentName(this, MyAdmin.class);
    if (dpm.isAdminActive(who)) {
      dpm.lockNow();// 鎖屏
      dpm.resetPassword("", 0);// 設(shè)置屏蔽密碼
      // 清除Sdcard上的數(shù)據(jù)
      // dpm.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE);
      // 恢復(fù)出廠設(shè)置
      // dpm.wipeData(0);
    } else {
      Toast.makeText(this, "還沒有打開管理員權(quán)限", 1).show();
      return;
    }
  }
  /**
   * 卸載當(dāng)前軟件
   */
  public void uninstall(View view) {
    // 1.先清除管理員權(quán)限
    ComponentName mDeviceAdminSample = new ComponentName(this,
        MyAdmin.class);
    dpm.removeActiveAdmin(mDeviceAdminSample);
    // 2.普通應(yīng)用的卸載
    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)根據(jù)API文檔可知,需要一個(gè)類繼承DeviceAdminReceiver:

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

(4)廣播接受者的設(shè)置(清單文件):

<?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>

運(yùn)行效果:

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

相關(guān)文章

  • Android自定義短信驗(yàn)證碼組件

    Android自定義短信驗(yàn)證碼組件

    這篇文章主要為大家詳細(xì)介紹了Android自定義短信驗(yàn)證碼組件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-01-01
  • Android 九宮格的實(shí)現(xiàn)方法

    Android 九宮格的實(shí)現(xiàn)方法

    今天在瀏覽網(wǎng)頁的時(shí)候看到了一篇有關(guān)九宮格實(shí)現(xiàn)的博文,感覺挺有意思。所以自己模仿做了一個(gè),先貼出代碼如下:
    2013-05-05
  • Android模擬實(shí)現(xiàn)網(wǎng)易新聞客戶端

    Android模擬實(shí)現(xiàn)網(wǎng)易新聞客戶端

    這篇文章主要為大家詳細(xì)介紹了Android模擬實(shí)現(xiàn)網(wǎng)易新聞客戶端,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • 一個(gè)簡單的Android軌跡動(dòng)畫

    一個(gè)簡單的Android軌跡動(dòng)畫

    這篇文章主要為大家詳細(xì)介紹了一個(gè)簡單的Android軌跡動(dòng)畫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • 詳解如何在Flutter中用小部件創(chuàng)建響應(yīng)式布局

    詳解如何在Flutter中用小部件創(chuàng)建響應(yīng)式布局

    由于Flutter的跨平臺、單一代碼庫的能力,了解屏幕管理以防止像柔性溢出錯(cuò)誤或糟糕的用戶界面設(shè)計(jì)這樣的問題是至關(guān)重要的。本文將探討如何用靈活和擴(kuò)展的小部件創(chuàng)建響應(yīng)式布局,需要的可以參考一下
    2022-02-02
  • 詳解Android .9.png “點(diǎn)九”圖片的使用

    詳解Android .9.png “點(diǎn)九”圖片的使用

    這篇文章主要為大家詳細(xì)介紹了Android .9.png “點(diǎn)九”圖片的使用方法,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Android自定義可控制速度的跑馬燈

    Android自定義可控制速度的跑馬燈

    這篇文章主要為大家詳細(xì)介紹了Android自定義可控制速度的跑馬燈,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • Android開發(fā)之AAR文件的生成與使用步驟

    Android開發(fā)之AAR文件的生成與使用步驟

    Android中的aar主要是針對于Android Library而言的,可以簡單的理解為是對Android Library的打包,這個(gè)包的格式為.aar,下面這篇文章主要給大家介紹了關(guān)于Android開發(fā)之AAR文件的生成與使用步驟的相關(guān)資料,需要的朋友可以參考下
    2022-07-07
  • Android Studio 3.0后出現(xiàn)AAPT2與“android.enableAapt2”問題的解決方法

    Android Studio 3.0后出現(xiàn)AAPT2與“android.enableAapt2”問題的解決方法

    這篇文章主要給大家介紹了關(guān)于Android Studio 3.0后出現(xiàn)AAPT2與“android.enableAapt2”問題的解決方法,文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-07-07
  • Android 中ListView的Item點(diǎn)擊事件失效的快速解決方法

    Android 中ListView的Item點(diǎn)擊事件失效的快速解決方法

    這篇文章主要介紹了Android 中ListView的Item點(diǎn)擊事件失效的快速解決方法的相關(guān)資料,需要的朋友可以參考下
    2016-09-09

最新評論