下面列出了android.app.Application#sendBroadcast ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@MainThread
private void broadcastInternal(T value, boolean foreground, boolean onlyInApp) {
logger.log(Level.INFO, "broadcast: " + value + " foreground: " + foreground +
" with key: " + key);
Application application = AppUtils.getApp();
if (application == null) {
logger.log(Level.WARNING, "application is null, you can try setContext() when config");
return;
}
Intent intent = new Intent(IpcConst.ACTION);
if (foreground && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
}
if (onlyInApp) {
intent.setPackage(application.getPackageName());
}
intent.putExtra(IpcConst.KEY, key);
boolean handle = ProcessorManager.getManager().writeTo(intent, value);
try {
if (handle) {
application.sendBroadcast(intent);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@MainThread
private void broadcastInternal(T value, boolean foreground, boolean onlyInApp) {
logger.log(Level.INFO, "broadcast: " + value + " foreground: " + foreground +
" with key: " + key);
Application application = AppUtils.getApp();
if (application == null) {
logger.log(Level.WARNING, "application is null, you can try setContext() when config");
return;
}
Intent intent = new Intent(IpcConst.ACTION);
if (foreground && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
}
if (onlyInApp) {
intent.setPackage(application.getPackageName());
}
intent.putExtra(IpcConst.KEY, key);
boolean handle = ProcessorManager.getManager().writeTo(intent, value);
try {
if (handle) {
application.sendBroadcast(intent);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public Notification setIconBadgeNum(@NonNull Application context, Notification notification, int count) throws Exception {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) {
throw new Exception(NOTIFICATION_ERROR);
}
Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
intent.putExtra("badge_count", count);
intent.putExtra("badge_count_package_name", context.getPackageName());
intent.putExtra("badge_count_class_name", Utils.getInstance().getLaunchIntentForPackage(context)); // com.test. badge.MainActivity is your apk main activity
// if (Utils.getInstance().canResolveBroadcast(context, intent)) {
context.sendBroadcast(intent);
// } else {
// throw new Exception(UNABLE_TO_RESOLVE_INTENT_ERROR_ + intent.toString());
// }
return notification;
}
@Test public void wifiStateChanges() {
Application application = RuntimeEnvironment.application;
TestSubscriber<Integer> o = new TestSubscriber<>();
RxWifiManager.wifiStateChanges(application).subscribe(o);
o.assertValues();
Intent intent1 = new Intent(WIFI_STATE_CHANGED_ACTION) //
.putExtra(EXTRA_WIFI_STATE, WIFI_STATE_DISABLED);
application.sendBroadcast(intent1);
o.assertValues(WIFI_STATE_DISABLED);
Intent intent2 = new Intent(WIFI_STATE_CHANGED_ACTION) //
.putExtra(EXTRA_WIFI_STATE, WIFI_STATE_UNKNOWN);
application.sendBroadcast(intent2);
o.assertValues(WIFI_STATE_DISABLED, WIFI_STATE_UNKNOWN);
}
@SuppressWarnings("ResourceType") @Test //
public void networkStateChanges() throws IllegalAccessException, InstantiationException {
Application application = RuntimeEnvironment.application;
TestSubscriber<NetworkStateChangedEvent> o = new TestSubscriber<>();
RxWifiManager.networkStateChanges(application).subscribe(o);
o.assertValues();
NetworkInfo networkInfo1 = NetworkInfo.class.newInstance();
WifiInfo wifiInfo1 = WifiInfo.class.newInstance();
Intent intent1 = new Intent(NETWORK_STATE_CHANGED_ACTION) //
.putExtra(WifiManager.EXTRA_NETWORK_INFO, networkInfo1)
.putExtra(WifiManager.EXTRA_BSSID, "foo")
.putExtra(WifiManager.EXTRA_WIFI_INFO, wifiInfo1);
application.sendBroadcast(intent1);
NetworkStateChangedEvent event1 =
NetworkStateChangedEvent.create(networkInfo1, "foo", wifiInfo1);
o.assertValues(event1);
NetworkInfo networkInfo2 = NetworkInfo.class.newInstance();
Intent intent2 = new Intent(NETWORK_STATE_CHANGED_ACTION) //
.putExtra(WifiManager.EXTRA_NETWORK_INFO, networkInfo2);
application.sendBroadcast(intent2);
NetworkStateChangedEvent event2 = NetworkStateChangedEvent.create(networkInfo2, null, null);
o.assertValues(event1, event2);
}
@SuppressWarnings("ResourceType") @Test //
public void supplicantStateChanges() throws IllegalAccessException, InstantiationException {
Application application = RuntimeEnvironment.application;
TestSubscriber<SupplicantStateChangedEvent> o = new TestSubscriber<>();
RxWifiManager.supplicantStateChanges(application).subscribe(o);
o.assertValues();
Intent intent1 = new Intent(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION) //
.putExtra(EXTRA_NEW_STATE, (Parcelable) SupplicantState.INACTIVE)
.putExtra(EXTRA_SUPPLICANT_ERROR, ERROR_AUTHENTICATING);
application.sendBroadcast(intent1);
SupplicantStateChangedEvent event1 =
SupplicantStateChangedEvent.create(SupplicantState.INACTIVE, ERROR_AUTHENTICATING);
o.assertValues(event1);
Intent intent2 = new Intent(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION) //
.putExtra(EXTRA_NEW_STATE, (Parcelable) SupplicantState.ASSOCIATING)
.putExtra(EXTRA_SUPPLICANT_ERROR, -1);
application.sendBroadcast(intent2);
SupplicantStateChangedEvent event2 =
SupplicantStateChangedEvent.create(SupplicantState.ASSOCIATING, -1);
o.assertValues(event1, event2);
}
@SuppressWarnings("ResourceType") @Test //
public void supplicantConnectionChanges() throws IllegalAccessException, InstantiationException {
Application application = RuntimeEnvironment.application;
TestSubscriber<Boolean> o = new TestSubscriber<>();
RxWifiManager.supplicantConnectionChanges(application).subscribe(o);
o.assertValues();
Intent intent1 = new Intent(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION) //
.putExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, true);
application.sendBroadcast(intent1);
o.assertValues(true);
Intent intent2 = new Intent(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION) //
.putExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false);
application.sendBroadcast(intent2);
o.assertValues(true, false);
}
@Test public void phoneStateChanges() {
Application application = RuntimeEnvironment.application;
TestSubscriber<PhoneStateChangedEvent> o = new TestSubscriber<>();
RxTelephonyManager.phoneStateChanges(application).subscribe(o);
o.assertValues();
Intent intent1 = new Intent(ACTION_PHONE_STATE_CHANGED) //
.putExtra(EXTRA_STATE, EXTRA_INCOMING_NUMBER)
.putExtra(EXTRA_INCOMING_NUMBER, "123-456-7890");
application.sendBroadcast(intent1);
PhoneStateChangedEvent event1 =
PhoneStateChangedEvent.create(EXTRA_INCOMING_NUMBER, "123-456-7890");
o.assertValues(event1);
Intent intent2 = new Intent(ACTION_PHONE_STATE_CHANGED).putExtra(EXTRA_STATE, EXTRA_STATE_IDLE);
application.sendBroadcast(intent2);
PhoneStateChangedEvent event2 = PhoneStateChangedEvent.create(EXTRA_STATE_IDLE, null);
o.assertValues(event1, event2);
}
@Test public void batteryStateChanges() {
Application application = RuntimeEnvironment.application;
TestSubscriber<BatteryState> o = new TestSubscriber<>();
RxBatteryManager.batteryChanges(application).subscribe(o);
o.assertValues();
Intent intent1 = new Intent(Intent.ACTION_BATTERY_CHANGED) //
.putExtra(BatteryManager.EXTRA_HEALTH, BatteryManager.BATTERY_HEALTH_COLD)
.putExtra(BatteryManager.EXTRA_ICON_SMALL, 0x3def2)
.putExtra(BatteryManager.EXTRA_LEVEL, 10)
.putExtra(BatteryManager.EXTRA_PLUGGED, 0)
.putExtra(BatteryManager.EXTRA_PRESENT, true)
.putExtra(BatteryManager.EXTRA_SCALE, 100)
.putExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_CHARGING)
.putExtra(BatteryManager.EXTRA_TECHNOLOGY, "unknown")
.putExtra(BatteryManager.EXTRA_TEMPERATURE, 40)
.putExtra(BatteryManager.EXTRA_VOLTAGE, 10000);
application.sendBroadcast(intent1);
BatteryState event1 =
BatteryState.create(BatteryHealth.COLD, 0x3def2, 10, 0, true, 100,
BatteryStatus.CHARGING, "unknown", 40, 10000);
o.assertValues(event1);
}
@Test public void subscribe() {
IntentFilter intentFilter = new IntentFilter("test_action");
Application application = RuntimeEnvironment.application;
TestSubscriber<Intent> o = new TestSubscriber<>();
Subscription subscription = RxBroadcastReceiver.create(application, intentFilter).subscribe(o);
o.assertValues();
Intent intent1 = new Intent("test_action").putExtra("foo", "bar");
application.sendBroadcast(intent1);
o.assertValues(intent1);
Intent intent2 = new Intent("test_action").putExtra("bar", "baz");
application.sendBroadcast(intent2);
o.assertValues(intent1, intent2);
Intent intent3 = new Intent("test_action_ignored");
application.sendBroadcast(intent3);
o.assertValues(intent1, intent2);
Intent intent4 = new Intent("test_action").putExtra("bar", "baz");
subscription.unsubscribe();
application.sendBroadcast(intent4);
o.assertValues(intent1, intent2);
}
@Override
public Notification setIconBadgeNum(@NonNull Application context, Notification notification, int count) throws Exception {
Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
intent.putExtra("badge_count", count);
intent.putExtra("badge_count_package_name", context.getPackageName());
intent.putExtra("badge_count_class_name", Utils.getInstance().getLaunchIntentForPackage(context));
if (Utils.getInstance().canResolveBroadcast(context, intent)) {
context.sendBroadcast(intent);
} else {
throw new Exception(UNABLE_TO_RESOLVE_INTENT_ERROR_ + intent.toString());
}
return notification;
}
/***nofity UI bundle installed***/
public static void notifyBundleInstalled(Application application) {
System.setProperty("BUNDLES_INSTALLED", "true");
application.sendBroadcast(new Intent(OpenAtlasInternalConstant.ACTION_BROADCAST_BUNDLES_INSTALLED));
}