發布時間: 2017-06-16 15:22:16
據不完全統計,現在市面上所有的Android手機都用電源鍵來手動鎖屏。一天之內手動鎖屏的次數絕對不少于30次。如果都用電源鍵來進行的話,那么電源鍵的使用壽命一定不會長久。雖然現在有一些桌面軟件都集成了一鍵鎖屏,但是要安裝這些軟件就必須安裝它們的其他組件,這點很不爽,因為它們會自動更新或者在后臺運行一些我不想要的東西。所以騰科小編決定自己寫一個鎖屏程序。經過在android開發文檔里面的查找,小編發現從android 2.2開始,API里面就包含了一個lockNow方法(android.app.admin.DevicePolicyManager包中),接下來,讓我們一起看看如何實現吧。
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. //激活管理員設備意圖 Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); //激活組件名 intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, who); //激活說明 intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,"激活我可以一鍵鎖屏"); startActivity(intent); } } |
怎么樣,學會了嗎?如果你有更好的辦法,歡迎你訪問騰科官網(http://sh.togogo.net)發表您的觀點。
上一篇: {甲骨文OCP}EXPDP報錯ORA-39171、ORA-01691解決方法
下一篇: {H5混合開發}組件tab選項卡