下面列出了android.app.Application#registerReceiver ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onCreate() {
super.onCreate();
Model.log(TAG, "onCreate");
Application application = getApplication();
mReceiver = new MissServiceBroadcastReceiver();
try {
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_MISS_WIDGET_REFRESH);
application.registerReceiver(mReceiver, filter);
} catch (Exception e) {
e.printStackTrace();
}
// Register Alarm
addAlarm(getApplication());
}
@Override
protected void after(XParam param) throws Throwable {
switch (mMethod) {
case onCreate:
// Install receiver for package management
if (PrivacyManager.isApplication(Process.myUid()) && !mReceiverInstalled)
try {
Application app = (Application) param.thisObject;
if (app != null) {
mReceiverInstalled = true;
Util.log(this, Log.INFO, "Installing receiver uid=" + Process.myUid());
app.registerReceiver(new Receiver(app), new IntentFilter(ACTION_MANAGE_PACKAGE),
PERMISSION_MANAGE_PACKAGES, null);
}
} catch (SecurityException ignored) {
} catch (Throwable ex) {
Util.bug(this, ex);
}
break;
}
}
public synchronized void initialize(@NonNull Application application) {
if (isInitialized()) {
return;
}
Inspector.requireNonNull(application, "application can't be");
this.application = application;
//检查是否支持BLE
if (!application.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
return;
}
//获取蓝牙配置器
BluetoothManager bluetoothManager = (BluetoothManager) application.getSystemService(Context.BLUETOOTH_SERVICE);
if (bluetoothManager == null || bluetoothManager.getAdapter() == null) {
return;
}
bluetoothAdapter = bluetoothManager.getAdapter();
//注册蓝牙开关状态广播接收者
if (broadcastReceiver == null) {
broadcastReceiver = new InnerBroadcastReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
filter.addAction(BluetoothDevice.ACTION_FOUND);
application.registerReceiver(broadcastReceiver, filter);
}
isInitialized = true;
}
@Override
public void afterHookedMethod(HookParam param) {
if (!HAS_REGISTER_LISENER) {
fristApplication = (Application) param.thisObject;
BroadcastReceiver broadcastReceiver = new CommandBroadcastReceiver();
fristApplication.registerReceiver(broadcastReceiver, new IntentFilter(CommandBroadcastReceiver.INTENT_ACTION));
HAS_REGISTER_LISENER = true;
}
}
/**
* Register for required broadcast receivers.
*/
private void registerBroadcastReceivers(@NonNull final Application application) {
application.registerReceiver(mBluetoothStateBroadcastReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
if (Utils.isMarshmallowOrAbove()) {
application.registerReceiver(mLocationProviderChangedReceiver, new IntentFilter(LocationManager.MODE_CHANGED_ACTION));
}
}
void registerReceiver() {
if (isRegisterReceiver) {
return;
}
Application application = AppUtils.getApp();
if (application != null) {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(IpcConst.ACTION);
application.registerReceiver(receiver, intentFilter);
isRegisterReceiver = true;
}
}
void registerReceiver() {
if (isRegisterReceiver) {
return;
}
Application application = AppUtils.getApp();
if (application != null) {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(IpcConst.ACTION);
application.registerReceiver(receiver, intentFilter);
isRegisterReceiver = true;
}
}
@Override
public void afterHookedMethod(HookParam param) {
// TODO Auto-generated method stub
if (!HAS_REGISTER_LISENER) {
fristApplication = (Application) param.thisObject;
IntentFilter filter = new IntentFilter(CommandBroadcastReceiver.INTENT_ACTION);
fristApplication.registerReceiver(new CommandBroadcastReceiver(), filter);
HAS_REGISTER_LISENER = true;
}
}
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
Log.d("cc", "after, register receiver");
if (!ModuleContext.HAS_REGISTER_LISENER) {
Application firstApplication = (Application) param.thisObject;
IntentFilter filter = new IntentFilter(CommandBroadcastReceiver.INTENT_ACTION);
firstApplication.registerReceiver(new CommandBroadcastReceiver(), filter);
ModuleContext.HAS_REGISTER_LISENER = true;
ModuleContext.getInstance().setFirstApplication(firstApplication);
Log.d("cc", "register over");
}
}
/**
* Register for required broadcast receivers.
*/
private void registerBroadcastReceivers(@NonNull final Application application) {
application.registerReceiver(bluetoothStateBroadcastReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
if (Utils.isMarshmallowOrAbove()) {
application.registerReceiver(locationProviderChangedReceiver, new IntentFilter(LocationManager.MODE_CHANGED_ACTION));
}
}
@Override
public void afterHookedMethod(HookParam param) {
// TODO Auto-generated method stub
if (!HAS_REGISTER_LISENER) {
fristApplication = (Application) param.thisObject;
IntentFilter filter = new IntentFilter(CommandBroadcastReceiver.INTENT_ACTION);
fristApplication.registerReceiver(new CommandBroadcastReceiver(), filter);
HAS_REGISTER_LISENER = true;
}
}
@Override
public void afterHookedMethod(HookParam param) {
// TODO Auto-generated method stub
if (!HAS_REGISTER_LISENER) {
fristApplication = (Application) param.thisObject;
IntentFilter filter = new IntentFilter(CommandBroadcastReceiver.INTENT_ACTION);
fristApplication.registerReceiver(new CommandBroadcastReceiver(), filter);
HAS_REGISTER_LISENER = true;
}
}
/**
* Enable hotswapping on an application instance. This call is not necessary if using {@link
* ExopackageApplication}. If enabling manually, make sure the app is set up to support the
* "modules" exopackage mode, and the initial call to {@link
* ExopackageDexLoader#loadExopackageJars(Context, boolean)} passed true as the second arg
*/
public static synchronized void setupHotswap(Application application) {
if (!sIsHotswapSetup) {
final File dexOptDir = application.getDir("exopackage_modular_dex_opt", Context.MODE_PRIVATE);
DelegatingClassLoader.getInstance().setDexOptDir(dexOptDir);
application.registerReceiver(
new ModularDexChangedReceiver(),
ModularDexChangedReceiver.getIntentFilter(application.getPackageName()));
sIsHotswapSetup = true;
}
}
public ConnectivityViewModel(@NonNull Application application) {
super(application);
application.registerReceiver(new NetworkChangeReceiver(), new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}