全国服务热线:400-885-2225
最新活动

【安卓入门与精通】一键锁屏学习

发布时间:2016-02-24 17:22 | 编辑:admin

 

 案例代码结构


3.2. 代码步骤

1. 编写MainActivity

package com.example.lockscreen;

 

import android.app.Activity;

import android.app.admin.DevicePolicyManager;

import android.os.Bundle;

import android.view.View;

 

public class MainActivity extends Activity {

 

private DevicePolicyManager deviceManager;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

deviceManager = (DevicePolicyManager)getSystemService(DEVICE_POLICY_SERVICE);

}

 

public void lockScreen(View view){

deviceManager.lockNow();

}

}

2. 编写activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<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.example.lockscreen.MainActivity>

    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:onClick="lockScreen"

        android:text="一键锁屏" />

 

</RelativeLayout>

3. 编写LockScreenReceiver

package com.example.receiver;

 

import android.app.admin.DeviceAdminReceiver;

import android.content.Context;

import android.content.Intent;

 

public class LockScreenReceiver extends DeviceAdminReceiver {

 

@Override

public void onReceive(Context context, Intent intent) {

// TODO Auto-generated method stub

super.onReceive(context, intent);

}

}

4. 编写AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.lockscreen"

    android:versionCode="1"

    android:versionName="1.0" >

    <uses-sdk

        android:minSdkVersion="14"

        android:targetSdkVersion="21" />

    <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="com.example.receiver.LockScreenReceiver"

            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>

5. 激活一键锁屏


6. 使用代码激活管理员设备

package com.example.lockscreen;

import android.app.Activity;

import android.app.admin.DevicePolicyManager;

import android.content.ComponentName;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import com.example.receiver.LockScreenReceiver;

public class MainActivity extends Activity {

private DevicePolicyManager deviceManager;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

deviceManager = (DevicePolicyManager)getSystemService(DEVICE_POLICY_SERVICE);

}

 

public void lockScreen(View view){

ComponentName who = new ComponentName(this, LockScreenReceiver.class);

if(deviceManager.isAdminActive(who)){

deviceManager.lockNow();

}else{

openAdminDevice(who);

}

}

 

private void openAdminDevice(ComponentName who) {

// TODO Auto-generated method stub

// Launch the activity to have the user enable our admin.

//激活管理员设备意图

        Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);

        //激活组件名

        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMINwho);

        //激活说明

        intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,"激活我可以一键锁屏");

        startActivity(intent);

}

}

 


 
点击QQ沟通