Android中的静态系统广播和动态系统广播

来源:互联网 发布:巨人网络收购投哪网 编辑:程序博客网 时间:2024/04/30 14:11

Android4.4:


静态广播:

可在AndroidManifest.xml中定义,不需程序启动即可接收,可用作自动启动程序


Intent.ACTION_BOOT_COMPLETED //系统启动完成

Intent.ACTION_MEDIA_MOUNTED //SD卡挂载

Intent.ACTION_MEDIA_UNMOUNTED //SD卡卸载

Intent.ACTION_USER_PRESENT//解除锁屏

ConnectivityManager.CONNECTIVITY_ACTION//网络状态变化

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <receiver android:name=".StaticBroadcastReceiver">  
  2.             <intent-filter>  
  3.                 <action android:name="android.intent.action.BOOT_COMPLETED" />  
  4.                 <category android:name="android.intent.category.HOME" />  
  5.             </intent-filter>  
  6.   
  7.             <intent-filter>  
  8.                 <action android:name="android.intent.action.MEDIA_MOUNTED"/>  
  9.                 <action android:name="android.intent.action.MEDIA_UNMOUNTED"/>  
  10.                 <category android:name="android.intent.category.DEFAULT" />  
  11.                 <data android:scheme="file" />  
  12.             </intent-filter>  
  13.   
  14.   
  15.             <intent-filter>  
  16.                 <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />  
  17.         <action android:name="android.intent.action.USER_PRESENT" />  
  18.             </intent-filter>  
  19. </receiver>  


[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public class StaticBroadcastReceiver extends BroadcastReceiver {  
  2.   
  3.     @Override  
  4.     public void onReceive(Context context, Intent intent) {  
  5.         // TODO Auto-generated method stub  
  6.         if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){  
  7.             Log.d(TAG, "onReceive boot: ");   
  8.             Intent new_intent = new Intent(context,TestLauncher.class);  
  9.             //popup the activity  
  10.             new_intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  11.             context.startActivity(new_intent);  
  12.         }else if(intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {  
  13.             Log.d(TAG, "onReceive ACTION_USER_PRESENT: ");  
  14.         }  
  15.     }  
  16.   
  17. }  

动态广播:

只能在代码中注册,程序适应系统变化做操作,程序运行状态才能接收到

Intent.ACTION_SCREEN_ON //屏幕亮

Intent.ACTION_SCREEN_OFF //屏幕灭

Intent.ACTION_TIME_TICK //时间变化  每分钟一次

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <span style="white-space:pre">    </span>    IntentFilter filter = new IntentFilter();  
  2.         filter.addAction(Intent.ACTION_SCREEN_ON);  
  3.         filter.addAction(Intent.ACTION_SCREEN_OFF);  
  4.         filter.addAction(Intent.ACTION_TIME_TICK);  
  5.         registerReceiver(new DynamicBroadcastReceiver(), filter);  
0 0
原创粉丝点击