1. 程式人生 > >Android註冊廣播

Android註冊廣播




程式碼動態註冊廣播
  1. private static final String ACTION = "com.receiver.umengevent.do";  
[java]  view plain  copy
  1. IntentFilter filter = 
    new IntentFilter();  
  2. filter.addAction(ACTION);  
  3. filter.setPriority(Integer.MAX_VALUE);  
  4. registerReceiver(myReceiver, filter);  

配置靜態註冊廣播         <receiver android:name="com.kk.dynamic.receiver.UMengEventReceiver" >              <intent-filter>                     <action android:name="com.receiver.umengevent.do" />              </intent-filter>         </receiver>

傳送廣播
public void sendReceiver(int c){ Intent intent= new Intent(); intent.setAction("com.receiver.umengevent.do"); intent.putExtra(UMengEventReceiver.keyName, c); context.sendBroadcast(intent); }


public class UMengEventReceiver extends BroadcastReceiver {
public static final int JarNum = 1000; @Override public void onReceive(Context context, Intent intent) { int what = intent.getIntExtra("keyName", 0); switch (what)  { case JarNum: onEvent(context, "JarNum"); break; } }
}


---------------------------------------------------------------------------

 新建一個服務獨立程序
      <service             android:name="com.kk.dynamic.service.MyService"             android:process=":remote" >         </service>

----------------------------------------------------
程式碼動態註冊廣播
  1. private static final String ACTION = "com.receiver.umengevent.do";  
[java]  view plain  copy
  1. IntentFilter filter = new IntentFilter();  
  2. filter.addAction(ACTION);  
  3. filter.setPriority(Integer.MAX_VALUE);  
  4. registerReceiver(myReceiver, filter);  

配置靜態註冊廣播         <receiver android:name="com.kk.dynamic.receiver.UMengEventReceiver" >              <intent-filter>                     <action android:name="com.receiver.umengevent.do" />              </intent-filter>         </receiver>

傳送廣播 public void sendReceiver(int c){ Intent intent= new Intent(); intent.setAction("com.receiver.umengevent.do"); intent.putExtra(UMengEventReceiver.keyName, c); context.sendBroadcast(intent); }


public class UMengEventReceiver extends BroadcastReceiver {
public static final int JarNum = 1000; @Override public void onReceive(Context context, Intent intent) { int what = intent.getIntExtra("keyName", 0); switch (what)  { case JarNum: onEvent(context, "JarNum"); break; } }
}


---------------------------------------------------------------------------

 新建一個服務獨立程序
      <service             android:name="com.kk.dynamic.service.MyService"             android:process=":remote" >         </service>

----------------------------------------------------