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

问题描述:

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

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

当屏幕关闭和打开并且应用程序已在运行时,我实际上在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.当您的应用程序启动时,您必须启动服务注册SCREEN_TURN_ONSCREEN_TURN_OFF events

原因如果您将无法启动服务来注册这些事件,只是注册它的activity.Then内活动时被摧毁它停止登记和开/关events.Making服务使它活得比屏幕活动或您的应用程序的生命周期。

2.Now你需要把一些代码在您的服务(onCreate方法内)

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

3.Make一个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.Now在服务onStartCommand方法把一些代码来获取通过接收器发送的标志,开始你的锁活动。

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

希望它可以帮助你。