怎样获得系统的广播,比如屏幕的关闭与开启广播

解决方案 »

  1.   

    屏幕被关闭 android.intent.action.SCREEN_OFF
    屏幕已经被打开 android.intent.action.SCREEN_ON 新建类MyReceiver.java,代码如下:package org.shuxiang.test;import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.util.Log;public class MyReceiver extends BroadcastReceiver
    {
    @Override 
     public void onReceive(Context context, Intent intent)
     {
    Log.i("Test", "收到广播");
                    //你需要增加的代码放在这里
     }

    在AndroidManifest.xml里面注册广播接收机
    <receiver android:name="MyReceiver"> 
    <intent-filter>
    <action android:name="android.intent.action.SCREEN_OFF" />
    <action android:name="android.intent.actionandroid.intent.action.SCREEN_ON" />
    </intent-filter>
    </receiver> 
      

  2.   

    楼上正解...只需要在XML文件里,把过滤器写好就行了