按电源按钮启动应用程序

2023-12-02

我找到了这个code按电源按钮启动应用程序。

我不知道为什么这个代码对我不起作用,但无论如何我正在将一些代码与清单一起放置,请检查...

然后告诉我通过按一次或两次电源按钮启动应用程序的代码是否正确?

   toast = Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT);

    btnToggleLock.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (btnToggleLock.isChecked()) {    

                    toast.cancel();
                    toast.setText("Unlocked");
                    toast.show();

                    Log.i("Unlocked", "If");

                    Context context = getApplicationContext();
                    KeyguardManager _guard = (KeyguardManager) context
                            .getSystemService(Context.KEYGUARD_SERVICE);
                    KeyguardLock _keyguardLock = _guard
                            .newKeyguardLock("KeyguardLockWrapper");
                    _keyguardLock.disableKeyguard();

                    PanicButtonActivity.this.startService(new Intent(
                            PanicButtonActivity.this, UpdateService.class));

                } else {

                    toast.cancel();
                    toast.setText("Locked");
                    toast.show();

                    Context context = getApplicationContext();
                    KeyguardManager _guard = (KeyguardManager) context
                            .getSystemService(Context.KEYGUARD_SERVICE);
                    KeyguardLock _keyguardLock = _guard
                            .newKeyguardLock("KeyguardLockWrapper");
                    _keyguardLock.reenableKeyguard();

                    Log.i("Locked", "else");

                    PanicButtonActivity.this.stopService(new Intent(PanicButtonActivity.this,
                            UpdateService.class));

                }

            }
        });

接收器.java:

public class Receiver extends BroadcastReceiver {

    private boolean screenOff;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            screenOff = true;
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            screenOff = false;
        }
        Intent i = new Intent(context, UpdateService.class);
        i.putExtra("screen_state", screenOff);
        context.startService(i);
    }

}

Service:

public class UpdateService extends Service {

    BroadcastReceiver mReceiver;

@Override
public void onCreate() {
    super.onCreate();
    // register receiver that handles screen on and screen off logic
    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    mReceiver = new Receiver();
    registerReceiver(mReceiver, filter);
}

@Override
public void onDestroy() {

    unregisterReceiver(mReceiver);
    Log.i("onDestroy Reciever", "Called");

    super.onDestroy();
}

@Override
public void onStart(Intent intent, int startId) {
    boolean screenOn = intent.getBooleanExtra("screen_state", false);
    if (!screenOn) {
        Log.i("screenON", "Called");
        Toast.makeText(getApplicationContext(), "Awake", Toast.LENGTH_LONG)
                .show();
    } else {
        Log.i("screenOFF", "Called");
    }
}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}
}

Manifest.xml:-

 <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>

   <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.panic.app.PanicButtonActivity"
        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.panic.app.Receiver" />
    <service android:name="com.panic.app.UpdateService" />

</application>

首先,与其他广泛传播的意图不同,您需要创建一个将继续运行的服务,如下所示:

public static class UpdateService extends Service {

        @Override
        public void onCreate() {
            super.onCreate();
            // register receiver that handles screen on and screen off logic
            IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
            filter.addAction(Intent.ACTION_SCREEN_OFF);
            BroadcastReceiver mReceiver = new Receiver();
            registerReceiver(mReceiver, filter);
        }

        @Override
        public void onStart(Intent intent, int startId) {
            boolean screenOn = intent.getBooleanExtra("screen_state", false);
            if (!screenOn) {
                // your code
            } else {
                // your code
            }
        }
}

你的接收器可以是某种东西

public class Receiver extends BroadcastReceiver {

    private boolean screenOff;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            screenOff = true;
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            screenOff = false;
        }
        Intent i = new Intent(context, UpdateService.class);
        i.putExtra("screen_state", screenOff);
        context.startService(i);
    }

}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

按电源按钮启动应用程序 的相关文章

随机推荐

  • Java 和 SQL:返回 null 还是抛出异常?

    这是另一个有争议的主题 但这次我只寻找简单且有记录的答案 场景 我们假设以下方法 我会避免以下情况 sql append SELECT FROM append dogs table sql append WHERE append colNa
  • 将 C++20 模板化 Lambda 传递给函数然后调用它

    我试图将模板化 lambda 传递给函数 然后使用模板参数调用它 以启用该函数针对自定义类型的专门化 但是当我尝试调用 lambda 时 出现此错误 error invalid operands to binary expression 对
  • 使用“or”的基本 Python If 语句

    我正在遵循一个学习 python 的初学者计划 我正在努力寻找一种更好的方法来使用具有多种可能性的 if 语句 我正在编写一个基于文本的基本游戏 用户可以选择三种不同的难度级别 简单 中等 困难 我只是想编写接受 1 2 或 3 的代码来告
  • 不允许我将编辑保存到 appsscript.json

    我想访问与用户 Google 帐户关联的名称来解决GoogleJsonResponseException API call to people people get failed with error The caller does not
  • 发送 GuzzleHttp\Psr7\Request 时 Guzzle6 错误资源类型无效:数组

    我试图使用 GuzzleHttp Psr7 Request 发送代码 不知何故我收到错误 Invalid resources type array 以下是我的代码 params name gt myName id gt myId clien
  • 为什么 JavaScript 中的“假”是真的?

    我知道在 javascript 中空字符串是假的 非空字符串在 javascript 中是真的 然而 为什么是 false javascript中的真相 规范中有什么明确的内容吗 这是性能问题还是在某些情况下您需要字符串 false 代表t
  • Aeson 合并对象编码

    我想解析和编写具有一些共同基本属性和一些附加单独属性的 JSON 对象 例如 假设我们有两种类型的对象User and Email 两种类型共享相同的基本属性foo and bar 但它们具有特定于其类型的附加属性 User foo foo
  • 将大量节点插入 Neo4J

    我有一个表存储在一个典型的 MySQL 数据库中 并且我使用 java 构建了一个小型解析器工具 用于解析并构建 neo4j 数据库 该数据库将有约 4000 万个节点 每个节点都有一个或多个边 最多可能有 10 个边 问题来自我必须创建某
  • 如何调用“Select”的OnChange事件? (Delphi - 网络浏览器)

    我正在使用 Delphi 和 Web 浏览器组件来导航 html 页面 该页面有一个 Combobox 有什么方法可以调用 OnChange 事件吗 组合框是这样的
  • 将数组列中的值替换为另一个表中的相关值

    在我的数据库中我有一个表relations有一个柱子relation ids包含用户的 ID user id 它采用具有多个可能 ID 的数组形式 例如 111 112 156 4465 我还有另一张桌子names包含有关用户的信息 例如u
  • 如何在 iPhone 上使用委托模式

    我对在 iPhone 上使用委托模式有一些疑问 这是使用委托模式的代码 这段代码有效 SecondViewController secondViewController SecondViewController alloc init sec
  • 如何仅向特定行授予 MySQL 权限

    假设有一张学生桌 学生 身份证号 姓名 城市 我想创建一个用户 A 并仅授予更新 id 10 的记录的权限 CREATE USER A GRANT UPDATE ON student TO A WHERE student id 10 我尝试
  • php检查数组中的多个日期是否在日期范围内

    我有一个结构如下的数组 Array 0 gt 24 12 2013 1 gt 25 12 2013 2 gt 26 12 2014 3 gt 27 12 2013 4 我想检查数组中的任何日期是否在给定的日期范围内 日期范围的结构如下 st
  • 我可以隐藏 UIDocumentInteractionController 视图上的操作按钮吗?

    我想知道是否可以隐藏 UIDocumentInteractionController 上的操作按钮 以便用户实际上无法在另一个应用程序中打开文档 我发现了一些对我来说足够有效的东西 BOOL documentInteractionContr
  • 如何设置自定义小部件的背景颜色和边框宽度?

    我有一个自定义小部件 其父级是另一个自定义小部件 我可以使用设置父自定义小部件的背景颜色QPalette而且效果很好 但我无法使用两者来设置子自定义小部件的边框颜色QPalette and stylesheet 这就是我设置父自定义小部件背
  • 使用 Azure Ad 中的服务应用程序限制对组或单个邮箱的访问

    我有一个 Azure 集成服务应用程序 守护程序应用程序 具有 Microsoft graph api 的权限 我现在可以读取整个公司的所有邮箱 这很棒 但可能会引起业务管理方面的一些担忧 我们在 Office 365 中使用 Outloo
  • StructureMap HowTo:深层对象的条件构造

    我很难有条件地创建依赖关系 谷歌搜索 我还没有找到使用 BuildStack 和条件谓词的好例子 这是我在注册表中所做的事情 snip public SomeRegistry this InstanceOf
  • 有什么可能的方法从 UIViewcontroller 类调用 drawRect 吗?

    我有一个UIViewController类称为AppController h AppController m 我在那里有数千行代码 这是我在询问之前没有测试它的唯一原因 有没有可能的方法来使用drawRect in a UIViewCont
  • 提高 Get-FileMetaData 的速度

    我目前正在使用以下来自 scriptingguys com 的脚本 这一切都归功于他们 我只是添加了最后两行 它需要一个目录并从文件的元数据中提取文件路径和注释字段 目前该脚本需要 1 5 分钟多一点才能完全运行 有没有办法加快速度或使用不
  • 按电源按钮启动应用程序

    我找到了这个code按电源按钮启动应用程序 我不知道为什么这个代码对我不起作用 但无论如何我正在将一些代码与清单一起放置 请检查 然后告诉我通过按一次或两次电源按钮启动应用程序的代码是否正确 toast Toast makeText get