發(fā)布時間: 2017-06-16 15:22:16
據(jù)不完全統(tǒng)計,現(xiàn)在市面上所有的Android手機(jī)都用電源鍵來手動鎖屏。一天之內(nèi)手動鎖屏的次數(shù)絕對不少于30次。如果都用電源鍵來進(jìn)行的話,那么電源鍵的使用壽命一定不會長久。雖然現(xiàn)在有一些桌面軟件都集成了一鍵鎖屏,但是要安裝這些軟件就必須安裝它們的其他組件,這點(diǎn)很不爽,因?yàn)樗鼈儠詣痈禄蛘咴诤笈_運(yùn)行一些我不想要的東西。所以騰科小編決定自己寫一個鎖屏程序。經(jīng)過在android開發(fā)文檔里面的查找,小編發(fā)現(xiàn)從android 2.2開始,API里面就包含了一個lockNow方法(android.app.admin.DevicePolicyManager包中),接下來,讓我們一起看看如何實(shí)現(xiàn)吧。
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(); } } |
<?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> |
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); } } |
<?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> |
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. //激活管理員設(shè)備意圖 Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); //激活組件名 intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, who); //激活說明 intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,"激活我可以一鍵鎖屏"); startActivity(intent); } } |
怎么樣,學(xué)會了嗎?如果你有更好的辦法,歡迎你訪問騰科官網(wǎng)(http://sh.togogo.net)發(fā)表您的觀點(diǎn)。