电源按钮按下android时打开活动

时间:2014-02-24 07:57:03

标签: android screen lockscreen

我正在尝试制作一个锁屏应用程序,这就是为什么我想在每次打开屏幕时启动我的应用程序。目前,我找到了一个解决方案,其中使用Receiver for Screen开/关以及方法onPause()onResume()。这是示例的链接:http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/#comment-4777

我使用的是活动的示例,而不是服务。

当屏幕关闭然后打开并且应用程序已经运行时,我实际上在LogCat中收到了反馈。问题是,当我打开屏幕时,应用程序无法启动(即使它显示在最近使用的应用程序下)。

我不确定,但我认为该示例工作正常,我只是缺少添加基本代码。

我希望有人能帮忙!!


感谢您的快速回答。我试图运行你的代码,但它仍然无法正常工作。我不确定是什么问题。没有错误消息。应用程序只是正常运行,但在屏幕亮起时无法启动。

为了更方便地帮助我,这是我的代码;)

这是我的接收者:

package com.example.screenlocker;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class StartMyServiceAtBootReceiver extends BroadcastReceiver {


public static boolean screenOff = true;

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



    } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
        screenOff = false;
    }
}

这是我的服务:

package com.example.screenlocker;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;

public class LockService extends Service {

public void onCreate(){
    IntentFilter filter=new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    BroadcastReceiver locker=new StartMyServiceAtBootReceiver();
    registerReceiver(locker, filter);
}

  @Override
  public void onStart(Intent intent, int startId) {
      boolean screenOn = intent.getBooleanExtra("screen_state", false);        
      if(screenOn){
                startActivity(new Intent(this, MainActivity.class));  

               }

  }

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

}

也许你知道出了什么问题。

1 个答案:

答案 0 :(得分:1)

为了实现这一目标,需要做以下事情。

1.当您的应用程序启动时,您必须启动服务以注册SCREEN_TURN_ONSCREEN_TURN_OFF events

原因如果您不启动服务来注册这些事件,只需在活动中注册它。然后当活动被销毁时,它会停止注册屏幕开/关事件。制作服务使它比活动或你的应用程序的生命周期更长。

2.现在您需要在服务中添加一些代码(在onCreate方法内)

IntentFilter filter=new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
locker=new startLockActivity();
registerReceiver(locker, filter);

3.制作BroadCastReciever并检查屏幕ON / OFF事件并相应地执行操作。

if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                screenOff = true;
                Intent i = new Intent(context,Locker.class);
                i.putExtra("screen_state", screenOff);
                context.startService(i);

            } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                screenOff = false;
            }

4.现在你的服务onStartCommand方法放了一些代码来通过接收器发送标志并开始锁定活动。

boolean screenOn = intent.getBooleanExtra("screen_state", false);        
if(screenOn){
          startActivty(this,yourLockActivity.class);    
         }

希望它可以帮助你。

相关问题