Android开源应用《守护按键》

本文介绍了一款名为“守护按键”的一键锁屏应用,该应用旨在保护手机电源键并节省电池电量。它通过自定义的软件锁屏机制来替代硬件锁屏功能,不仅对硬件无损害,而且完全免费且开源。文中提供了应用的工作原理、源代码链接及安装包下载地址。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

同步自:http://www.elitor.org/?p=225

守护按键,其实就是一键锁屏软件实现软件关屏,而不用通过电源键关屏,节省电池电量。
特点:
1.绝对对硬件无损伤,保护您的电源按键。
2.绝对小巧无广告。
3.绝对免费,同时开源。

已经发布在安卓市场,欢迎下载使用。地址是:http://static.apk.hiapk.com/html/2012/05/595025.html?module=256&info=iFukYgljLpU%3D

已经托管在github:https://github.com/elitor/key-guardian


MyLockScreen.java

package org.elitor.lockscreen;
 
import org.elitor.lockscreen.R;
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
 
public class MyLockScreen extends Activity
{
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.d("Init","yes");
 
        //获得android设备管理代理
        DevicePolicyManager localDCM = (DevicePolicyManager)getSystemService("device_policy");
        ComponentName localCompName = new ComponentName(this, AdminReceiver.class);
 
        if (localDCM.isAdminActive(localCompName))
        {
            localDCM.lockNow();
            finish();
        }
        else //第一次运行
        {
            Intent localIntent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            localIntent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, localCompName);
            localIntent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,"need to active");
            startActivityForResult(localIntent, 1);
            finish();
        }
 
    }
 
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        System.exit(0);
    }
 
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        System.exit(0);
    }
 
}

AdminReceiver.java


package org.elitor.lockscreen;
 
import android.app.admin.DeviceAdminReceiver;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
 
public class AdminReceiver extends DeviceAdminReceiver
{
    @Override
    public DevicePolicyManager getManager(Context context) {
        return super.getManager(context);
    }
    @Override
    public ComponentName getWho(Context context) {
        return super.getWho(context);
    }
 
    /**
     * 激活
     */
    @Override
    public void onEnabled(Context context, Intent intent) {
        Toast.makeText(context, "启动设备管理", Toast.LENGTH_SHORT).show();
 
        super.onEnabled(context, intent);
    }
 
    /**
     * 禁用
     */
    @Override
    public void onDisabled(Context context, Intent intent) {
        Toast.makeText(context, "禁用设备管理", Toast.LENGTH_SHORT).show();
 
        super.onDisabled(context, intent);
    }
    @Override
    public CharSequence onDisableRequested(Context context, Intent intent) {
        return super.onDisableRequested(context, intent);
    }
 
    @Override
    public void onPasswordChanged(Context context, Intent intent) {
        super.onPasswordChanged(context, intent);
    }
    @Override
    public void onPasswordFailed(Context context, Intent intent) {
        super.onPasswordFailed(context, intent);
    }
    @Override
    public void onPasswordSucceeded(Context context, Intent intent) {
        super.onPasswordSucceeded(context, intent);
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
    }
    @Override
    public IBinder peekService(Context myContext, Intent service) {
        return super.peekService(myContext, service);
    }
 
}

在main.xml中添加两个按键,用于激活或者取消激活设备管理。

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/btn_enabledevice" />
 
<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/btn_disabledevice" />

在Manifest.xml中注册receiver.

<receiver
    android:name=".AdminReceiver"
    android:icon="@drawable/android_5"
    android:description="@string/description"
    android:label="@string/labelValue"
    android:permission="android.permission.BIND_DEVICE_ADMIN"
    >
     <meta-data
        android:name="android.app.device_admin"
        android:resource="@xml/lockscreen"/>
    <intent-filter>
        <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
    </intent-filter>
</receiver>

meta-data用到资源文件lockscreen.xml,内容如下:

<?xml version="1.0" encoding="utf-8"?>
 
<device-admin
  xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-policies>
        <!-- 强行锁定 -->
        <force-lock />
    </uses-policies>
</device-admin>


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值