Android註冊廣播
阿新 • • 發佈:2018-12-15
程式碼動態註冊廣播
- private static final String ACTION = "com.receiver.umengevent.do";
- IntentFilter filter =
- filter.addAction(ACTION);
- filter.setPriority(Integer.MAX_VALUE);
- registerReceiver(myReceiver, filter);
配置靜態註冊廣播 <receiver android:name="com.kk.dynamic.receiver.UMengEventReceiver" > <intent-filter> <action android:name="com.receiver.umengevent.do" /> </intent-filter> </receiver>
傳送廣播
public class UMengEventReceiver extends BroadcastReceiver {
}
---------------------------------------------------------------------------
新建一個服務獨立程序
<service android:name="com.kk.dynamic.service.MyService" android:process=":remote" > </service>
----------------------------------------------------
程式碼動態註冊廣播
- private static final String ACTION = "com.receiver.umengevent.do";
- IntentFilter filter = new IntentFilter();
- filter.addAction(ACTION);
- filter.setPriority(Integer.MAX_VALUE);
- 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>
----------------------------------------------------