Screen_Off在应用程序级别中不起作用

时间:2013-04-05 21:50:44

标签: android broadcastreceiver

Screen_Off在应用程序级别中不起作用,尽管它正在处理活动级别。

我的问题,当屏幕熄灭时,它必须进入登录屏幕,在代码下方,成功进入登录屏幕,但它导致我未注册接收广播

我做了什么

如果l在Menifesty级别,屏幕关闭广播不会触发,

舱单级别

          <receiver android:name="com.android.libray.ScreenReciver" android:enabled="true" >
         <intent-filter>
             <action android:name="android.intent.action.SCREEN_OFF"/>
                   <action android:name="android.intent.action.SCREEN_ON"/>
             </intent-filter>
      </receiver>


if i do in activity level,then it will fire screenReciver


  IntentFilter filter =new IntentFilter(Intent.ACTION_SCREEN_OFF);

            BroadcastReceiver mReceiver=new ScreenReciver();
                registerReceiver(mReceiver, filter);

public class ScreenReciver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
         if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                // do whatever you need to do here

             Intent myintent = new Intent(context,TimerClockActivity.class);
                myintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
                myintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(myintent);
                wasScreenOn = false;
             Log.v("screenoff", "Logggoff");

            } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                // and do whatever you need to do here

            }


    }

}

1 个答案:

答案 0 :(得分:1)

SCREEN_OFF广播是受保护的广播,如果您通过服务或活动或其他接收器中的Java代码动态注册,则只会收到广播。在清单中注册不适用于此广播。