关闭

Android注册广播

标签: android
123人阅读 评论(0) 收藏 举报
分类:



代码动态注册广播
  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>


----------------------------------------------------
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:11907次
    • 积分:255
    • 等级:
    • 排名:千里之外
    • 原创:12篇
    • 转载:0篇
    • 译文:2篇
    • 评论:1条
    文章分类
    文章存档