在Android中打开和关闭屏幕

问题描述:

对于我尝试生成的Android代码,我遇到了一些麻烦。我想要做的只是打印出一个1,如果用户想要解锁他们的手机,并且程序第一次启动时,以及0当用户按下屏幕锁定来锁定他们的手机。我认为这与Android生命周期有关,所以我尝试使用onPause和onResume,但是我的程序只打印出0,从不打1。 logTime方法简单地输出1或0,MainActivity中的onCreate方法调用onResume()一次。这里是我的代码:在Android中打开和关闭屏幕

MainActivity:

protected void onPause(){ 
    if(ScreenReceiver.screenOn){ 
     logTime(ScreenReceiver.screenOn); 
super.onPause(); 
} 

protected void onResume() 
    if(!ScreenReceiver.screenOn){ 
    logTime(!ScreenReceiver.screenOn); 
super.onResume(); 
} 

屏幕接收器:

public class ScreenReceiver extends BroadcastReceiver{ 
public static boolean screenOn; 


@Override 
public void onReceive(Context context, Intent intent){ 
    if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){ 
     screenOn = false; 
    } 
    else if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)){ 
     screenOn = true; 
    } 

} 

}

我不知道为什么只打印出0。可能有人知道为什么?谢谢!

+0

''logTime Description该条应该只是'logTime Description该条(ScreenReceiver.screenOn);'没有'' – 2013-02-24 01:16:50

+0

@ A - Ç嗯好(ScreenReceiver.screenOn!)!我现在遇到了一些麻烦 - 每当我恢复我的应用程序时,我都无法登录1.当我解锁屏幕时,没有任何内容被打印,但是当我锁定屏幕时,它会打印出0。为什么? – user200081 2013-02-24 01:25:16

+0

你在哪里注册/取消注册Receiver?除了调用具有基本相同参数的logTime()外,你有什么看起来是正确的。 – 2013-02-24 01:28:20

有你的活动onPause()和实际收到的广播之间存在延迟,所以通常你可以得到“奇怪”(错误)的读数,因为变量变化没有发生。该接收器需要动态注册,并且即使屏幕关闭并重新打开,您也希望它能够接收。通常,接收者在onPause()中未注册以避免泄漏,但在这里,我们将使用onDestroy(),因为这是我们可以使用的最后一个方法调用。为了简单起见,我在“活动”中将BroadcastReceiver设置为正确,并直接调用logTime()

示例代码:

public class MainActivity extends Activity { 

    BroadcastReceiver receiver; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); 
    filter.addAction(Intent.ACTION_SCREEN_OFF); 
    receiver = new BroadcastReceiver(){ 

     @Override 
     public void onReceive(Context arg0, Intent arg1) { 
     if(arg1.getAction().equals(Intent.ACTION_SCREEN_OFF)){ 
      logTime(false); 
     } 
     else if(arg1.getAction().equals(Intent.ACTION_SCREEN_ON)){ 
      logTime(true); 
     } 
     } 
    }; 
    registerReceiver(receiver, filter); 
    } 

    @Override 
    protected void onDestroy() 
    { 
    try{ 
     unregisterReceiver (receiver); 
    } 
    catch (NullPointerException e){ 
     e.printStackTrace(); 
    } 
    catch (IllegalStateException e1){ 
     e.printStackTrace(); 
    } 
    super.onDestroy(); 
    } 


    public void logTime (boolean screen) 
    { 
    Time now = new Time(); 
    now.setToNow(); 

    String lsNow = now.format("%m-%d-%Y %I:%M:%S"); 
    TextView myText = (TextView) findViewById (R.id.myText); 
    myText.append (" " + lsNow + (screen ? "0" : "1")); 
    } 
} 
+0

快速的问题 - 接收器应该是什么?它是什么类型的变量? – user200081 2013-02-24 02:15:40

+0

@ user200081'BroadcastReceiver接收器;'它只是一个BroadcastReceiver。我会说,而不是合并我的示例与您的代码,只需将内容视图设置为您在活动中使用的'layout' xml以检查我的示例是否可用。正如你所看到的,我摆脱了'onPause()/ onResume()',因为它们不再需要。 – 2013-02-24 02:18:19

+0

工程就像一个魅力。非常感谢你的帮助! – user200081 2013-02-24 02:39:55

一切似乎正确的根据你的代码,它应该总是打印0 ...

当屏幕关闭时,screenOn设置为false,你的活动的被调用时,它打印0

当应用程序正在运行,onResume被调用,screenOntrue(屏幕是否刚刚开启与否),在那里,你登录的screenOn相反,因此将再次为0 ...