【问题标题】:open activity when power button pressed android按下电源按钮时打开活动android
【发布时间】:2014-02-24 07:57:03
【问题描述】:

我正在尝试制作一个锁屏应用程序,这就是为什么我想在每次打开屏幕时启动我的应用程序。目前我找到了一个解决方案,其中使用了 Receiver for Screen on/off 和 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;
}

}

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

【问题讨论】:

    标签: android screen lockscreen


    【解决方案1】:

    为了达到这个目的,需要做以下事情。

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

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

    2.现在你需要在你的服务中加入一些代码(在onCreate方法里面)

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

    3.制作一个广播接收器并检查屏幕开/关事件并执行相应的操作。

    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);    
             }
    

    希望对你有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-17
      • 2011-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-10
      相关资源
      最近更新 更多