android亮屏,暗屏,解锁

监听屏幕SCREEN_ONSCREEN_OFF这两个action,实现屏幕锁定状态的监听,从而实现自己的锁屏效果,奇怪的是,这两个action只能通过代码的形式注册,才能被监听到,使用AndroidManifest.xml 完全监听不到。查了一下,发现这是PowerManager那边在发这个广播的时候,做了限制,限制只能有register到代码中的receiver才能接收。

package com.lock.muskmelon;

import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class Lock8secondActivity extends Activity {

  public void onCreate(Bundle savedInstanceState) {  

  super.onCreate(savedInstanceState);  

  setContentView(R.layout.main);  
  
  
        final String TAG = null;
  
       
  
  final IntentFilter filter = new IntentFilter(); 
        filter.addAction(Intent.ACTION_SCREEN_OFF); 
        filter.addAction(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_USER_PRESENT);
         
        final BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() { 
            @Override 
            public void onReceive(final Context context, final Intent intent) {
             

                String action = intent.getAction(); 

               
               if(Intent.ACTION_SCREEN_ON.equals(action))
               { 
                    Log.d(TAG, "screen is on..."); 
               
               }
              
               else if(Intent.ACTION_SCREEN_OFF.equals(action))
               { 
                    Log.d(TAG, "screen is off..."); 
               } 
              
               else if(Intent.ACTION_USER_PRESENT.equals(action))
               {
                Log.d(TAG, "screen is unlock...");
               }
              
            } 
        }; 
       
        registerReceiver(mBatInfoReceiver, filter);
  
  
  
 }  

}

扫描二维码关注公众号,回复: 704118 查看本文章

猜你喜欢

转载自f059074251.iteye.com/blog/1834967