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

android實(shí)現(xiàn)一鍵鎖屏和一鍵卸載的方法實(shí)例

 更新時(shí)間:2018年05月08日 08:38:01   作者:Hacker_Guo  
這篇文章主要給大家介紹了關(guān)于android如何實(shí)現(xiàn)一鍵鎖屏和一鍵卸載的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。

前言

本文主要介紹了關(guān)于android實(shí)現(xiàn)一鍵鎖屏和一鍵卸載的相關(guān)內(nèi)容,分享出來(lái)供大家參考學(xué)習(xí),這兩個(gè)功能也是大家在開(kāi)發(fā)中會(huì)遇到的兩個(gè)需求,下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧。

一.設(shè)備管理器操作步驟

1.創(chuàng)建類(lèi)DeviceAdminReceiver的子類(lèi)

如:com.itheima62.lockscreen.DeviceAdminSample

2.在清單文件中配置廣播接收者

<receiver
  android:name="com.itheima62.lockscreen.DeviceAdminSample"
  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>

3.配置字符串相關(guān)信息

 <string name="activity_sample_device_admin">設(shè)備管理員</string>
<string name="sample_device_admin">管理員</string>
<string name="sample_device_admin_description">開(kāi)啟設(shè)備管理員,不開(kāi)啟扣2000塊</string>

4.在res目錄下創(chuàng)建xml文件夾,在該文件夾下創(chuàng)建device_admin_sample.xml文件,內(nèi)容:

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

5.在代碼中創(chuàng)建設(shè)備管理器和組件

dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
 ComponentName who = new ComponentName(this, DeviceAdminSample.class);

6.寫(xiě)功能

dpm.lockNow();一鍵鎖屏

二.源代碼

創(chuàng)建類(lèi)DeviceAdminReceiver的子類(lèi)

package com.example.suoping;
import android.app.admin.DeviceAdminReceiver;
public class DeviceAdminSample extends DeviceAdminReceiver
{

}

MainActivity

package com.example.suoping;

import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;

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);
  lockScreen(null);
 }

 /**
  * @param v
  * 一鍵鎖屏
  */
 public void lockScreen(View v)
 { 
  //如果沒(méi)有激活設(shè)備管理員,提醒給用戶(hù)做事
  ComponentName who = new ComponentName(this, DeviceAdminSample.class);
  if (dpm.isAdminActive(who))
  {
   dpm.lockNow();//一鍵鎖屏
   finish();
  }
  else 
  {

    Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
    intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, who);
    intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
      "設(shè)備管理器,,,,,,,,,,,,,,,,");
    startActivityForResult(intent, 1);
  }
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) 
 {
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }
}

RemoveActivity

package com.example.suoping;

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;

public class RemoveActivity extends Activity
{
 @Override
 protected void onCreate(Bundle savedInstanceState) 
 {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  remove(null);

 }

 /**
  * 一鍵卸載
  * @param v
  */
 public void remove(View v)
 { 
  // 取消激活設(shè)備管理
  DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
  ComponentName who = new ComponentName(this, DeviceAdminSample.class);
  dpm.removeActiveAdmin(who);//取消激活管理設(shè)備

  //卸載
  Intent remove = new Intent("android.intent.action.DELETE");
  remove.addCategory("android.intent.category.DEFAULT");
  remove.setData(Uri.parse("package:" + getPackageName()));
  startActivity(remove);//卸載用戶(hù)apk的界面
 }
}

布局文件

MainActivity.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"
 android:background="#00000000">
</RelativeLayout>

RemoveActivity.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"
 android:background="#00000000"
 >
</RelativeLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.example.suoping"
 android:versionCode="1"
 android:versionName="1.0" >
 <uses-sdk
  android:minSdkVersion="8"
  android:targetSdkVersion="18" />

 <application
  android:allowBackup="true"
  android:icon="@drawable/ic_launcher"
  android:theme="@style/AppTheme" >
  <activity
   android:name="com.example.suoping.MainActivity"
   android:label="一鍵鎖屏" >
   <intent-filter>
    <action android:name="android.intent.action.MAIN" />

    <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>
  </activity>
   <activity
   android:name="com.example.suoping.RemoveActivity"
   android:label="一鍵卸載" >
   <intent-filter>
    <action android:name="android.intent.action.MAIN" />

    <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>
  </activity>

  <receiver
   android:name="com.example.suoping.DeviceAdminSample"
   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>

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>

strings.xml

<resources>
 <string name="app_name">一鍵鎖屏</string>
 <string name="action_settings">Settings</string>
 <string name="hello_world">Hello world!</string>
 <string name="activity_sample_device_admin">設(shè)備管理員</string>
 <string name="sample_device_admin">管理員</string>
 <string name="sample_device_admin_description">開(kāi)啟設(shè)備管理員,不開(kāi)啟扣2000塊</string>
</resources>

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

最新評(píng)論