动态注册BroadcastReceiver

1. [代码][Java]代码

package com.zjt.innerreceiver;

import android.app.Service;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.content.IntentFilter;

import android.os.IBinder;

import android.util.Log;

public class ServiceTest extends Service{

public final static String TAG = "ServiceTest";

private Receiver mReceiver;

@Override

public IBinder onBind(Intent arg0) {

return null;

}

@Override

public void onCreate() {

super.onCreate();

//注册Receiver

mReceiver = new Receiver(this);

mReceiver.registerActionAndScheme(Intent.ACTION_MEDIA_EJECT, "file");

mReceiver.registerActionAndScheme(Intent.ACTION_MEDIA_REMOVED, "file");

mReceiver.registerActionAndScheme(Intent.ACTION_MEDIA_MOUNTED, "file");

mReceiver.registerActionAndScheme(Intent.ACTION_PACKAGE_REMOVED, "package");

mReceiver.registerActionAndScheme(Intent.ACTION_PACKAGE_ADDED, "package");

}

@Override

public void onDestroy() {

super.onDestroy();

//注销Receiver

unregisterReceiver(mReceiver);

}

class Receiver extends BroadcastReceiver {

Context mContext;

public Receiver(Context context){

mContext = context;

}

//动态注册

public void registerAction(String action){

IntentFilter filter = new IntentFilter();

filter.addAction(action);

mContext.registerReceiver(this, filter);

}

public void registerActionAndScheme(String action, String dataScheme){

IntentFilter filter = new IntentFilter();   http://www.huiyi8.com/css3/

filter.addAction(action);      css3教程

filter.addDataScheme(dataScheme);

mContext.registerReceiver(this, filter);

}

@Override

public void onReceive(Context context, Intent intent) {

String action = intent.getAction();

Log.d(TAG, "action:" + action);

if(action.equals(Intent.ACTION_MEDIA_EJECT)) {

Log.d(TAG , "sdcard has been ejected");

} else if (action.equals(Intent.ACTION_MEDIA_REMOVED)) {

Log.d(TAG , "sdcard has been removed");

} else if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {

Log.d(TAG , "sdcard has been mounted");

} else if (action.equals(Intent.ACTION_PACKAGE_REMOVED)) {

Log.d(TAG, "Intent.ACTION_PACKAGE_REMOVED");

} else if (action.equals(Intent.ACTION_PACKAGE_ADDED)) {

Log.d(TAG, "Intent.ACTION_PACKAGE_ADDED");

}

}

}

}

动态注册BroadcastReceiver

时间: 2024-10-09 14:03:30

动态注册BroadcastReceiver的相关文章

Android:动态注册BroadcastReceiver

注释直接写在代码里,比较直观,我觉得这些定义系统的广播的宏,不是很好使: MainActivity.java: package com.vrinux.broadcastdemo; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.Int

BroadcastReceiver 的动态注册和静态注册问题

最近开发的小项目涉及到notification通知栏的交互问题,通知栏的各种点击事件响应通过使用broadcast实现,由于一开始没有想到使用handler提交通知栏执行后的状态或者说是信息.一直纠结与怎么注册broadcast的问题,这里先说一下注册的事情. 众所周知,注册可以在manifest.xml里面注册,还有就是在代码里面注册. 在manifest里面的注册形式是这样子的: <receiver android:name=".继承broadcastReceiver的class&qu

Android学习笔记(十一)BroadcastReceiver动态注册、注销示例

在上一篇博文中简单介绍了一下BroadcastReceiver的相关知识点,本篇举一个在代码中动态的注册.注销BroadcastReceiver的栗子. 1.首先创建一个MyReceiver并继承BroadcastReceiver,既然要动态注册该receiver的话,就无需在AndroidManifest.xml中配置它了: 1 public class MyReceiver extends BroadcastReceiver { 2 public static final String AC

BroadcastReceiver的使用,动态注册和注销,优先级和中断控制

BroadcastReceiver: BroadcastReceiver(广播接收器)是Android中的四大组件之一,用来通知某些事件的相关信息,如下载完成,设置改变等. 默认的BroadcastReceiver状态(新建完未更改任何设置)的简单使用方法: 1.通过new Intnet(MainActivity.this,MyReceiver.class)来启动指明的广播对象,(当manifests中注册了此BroadcastReceiver才能通过这种方法启动) 2.Intent可以通过in

动态注册和注销BroadercasterReceive(二)

有时候不需要监听广播是否有消息,所以可以使用动态注册的方式.即不需要再androidmanifest.xml中注册. 主要代码如下: package com.example.yabushan.broadercastreceiver; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class MyReceiver exte

Android 系统广播动态注册

package com.broadcasttest; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.view.View;

动态注册广播接收器,监听网络变化

1:NetChangeReceiver.java public class NetChangeReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)

动态注册广播,监听网络状态变化,判断网络模式

注册广播监听网络连接和断开状态的变化,判断网络连接模式: private ConnectivityManager mConnectivityManager; private NetworkInfo netInfo; /////////////监听网络状态变化的广播接收器 private BroadcastReceiver myNetReceiver = new BroadcastReceiver() { @Override public void onReceive(Context contex

Android:静态注册BroadcastReceiver

动态注册的广播接收器可以自由地控制注册与注销,在灵活性方面有很大的优势,但是它也存在着一个缺点,即必须要在程序启动之后才能接收到广播,因为注册的逻辑是写在onCreate()方法中的.那么有没有什么办法可以让程序在未启动的情况下就能接收到广播呢?这就需要使用静态注册的方式了. 下面附上静态注册的代码: MainActivity.java: package com.example.staticbroadcastreceiverdemo; import android.app.Activity; i