【问题标题】:Android Power Button Pressed按下 Android 电源按钮
【发布时间】:2019-11-29 15:11:13
【问题描述】:

我正在尝试创建一个可以在按下电源按钮时做出响应的应用程序。更具体地说,它会在按下 2 或 3 次时响应。

目前,我尝试了以下方法:

public class SMSKey extends BroadcastReceiver{

    static int countPowerOff = 0;
    private Activity activity = null;
    public SMSKey(Activity activity){
        this.activity = activity;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
            countPowerOff++;
        }else if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
            if(countPowerOff == 2){
                Intent i = new Intent(activity, SMSOptions.class);
                activity.startActivity(i);
            }
        }
    }

}

在我的清单中:

<receiver android:name=".SMSKey">
        <intent-filter >
            <action android:name="android.intent.action.SCREEN_OFF"/>
            <action android:name="android.intent.action.SCREEN_ON"/>
            <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
            <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
            <action android:name="android.intent.action.ACTION_SHUTDOWN"/>
        </intent-filter>
    </receiver>

终于在我的MainActivty.java中:

IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
SMSKey mReceiver = new SMSKey(this);
registerReceiver(mReceiver, filter);

即使这有效,它也只适用于第 1 次,当按下电源按钮时,它不会在第 2 次或第 3 次尝试时有效。为什么呢 ??

还有一个问题:如您所见,我在 MainActivity 中使用了这个 KeyPress 事件,这意味着应用程序将一直处于打开状态。有没有其他方法可以在不进入 MainActivity 的情况下实现这一点。

【问题讨论】:

    标签: android


    【解决方案1】:

    这甚至不是 Android 问题。在收到 3 次按键后,您永远不会重置您的 countPowerOff 变量。即使在这样做之后,您也必须考虑添加一个警报,该警报将在一些小超时后将您的 countPowerOff 变量重置为零。它将允许您避免用户不打算与您的应用程序交互而只是按下按钮但仍被计数的情况。

    关于你的第二个问题,尝试实现IntentService

    【讨论】:

    • 如何重置这个?当 countPowerOff 等于 3 时,我是否将其声明为零?关于这个超时。那是怎么做的?我有点新,所以请不要介意这个问题。
    • 是的,您在if(countPowerOff == 2) { /*...*/ } 语句中将变量设置为零。对于超时,我想您可以使用Handler
    【解决方案2】:

    解决办法

    public class MyReceiver extends BroadcastReceiver {
    private static int countPowerOff = 0;
    
    public MyReceiver (){
    
    }
    
    @Override
    public void onReceive(Context context, Intent intent){
    if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){    
        Log.e("In on receive", "In Method:  ACTION_SCREEN_OFF");
        countPowerOff++;
    }
    else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
        Log.e("In on receive", "In Method:  ACTION_SCREEN_ON");
    }
    else if(intent.getAction().equals(Intent.ACTION_USER_PRESENT)){
        Log.e("In on receive", "In Method:  ACTION_USER_PRESENT");
        if (countPowerOff >= 2)
        {
            countPowerOff=0;
            Toast.makeText(context, "MAIN ACTIVITY IS BEING CALLED ", Toast.LENGTH_LONG).show();
            Intent i = new Intent(context, About.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
            context.startActivity(i);
        }
    }
      }
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-29
      • 2014-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多