下面列出了android.content.IntentFilter#addCategory ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Ensures intents with {@link CustomTabsService#ACTION_CUSTOM_TABS_CONNECTION} resolve to a
* Service with given categories
*/
public static void installCustomTabsService(String providerPackage, List<String> categories) {
Intent intent = new Intent()
.setAction(CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION)
.setPackage(providerPackage);
IntentFilter filter = new IntentFilter();
for (String category : categories) {
filter.addCategory(category);
}
ResolveInfo resolveInfo = new ResolveInfo();
resolveInfo.filter = filter;
ShadowPackageManager manager = Shadows.shadowOf(RuntimeEnvironment.application
.getPackageManager());
manager.addResolveInfoForIntent(intent, resolveInfo);
}
public void testShowBasicShareDialog() {
final WritableMap content = new WritableNativeMap();
content.putString("message", "Hello, ReactNative!");
final WritableMap options = new WritableNativeMap();
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_CHOOSER);
intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
ActivityMonitor monitor = getInstrumentation().addMonitor(intentFilter, null, true);
getTestModule().showShareDialog(content, options);
waitForBridgeAndUIIdle();
getInstrumentation().waitForIdleSync();
assertEquals(1, monitor.getHits());
assertEquals(1, mRecordingModule.getOpened());
assertEquals(0, mRecordingModule.getErrors());
}
public void initBluetooth() {
if (!ensureInit()) {
Log.w("[Bluetooth] Manager tried to init bluetooth but LinphoneService not ready yet...");
return;
}
IntentFilter filter = new IntentFilter();
filter.addCategory(BluetoothHeadset.VENDOR_SPECIFIC_HEADSET_EVENT_COMPANY_ID_CATEGORY + "." + BluetoothAssignedNumbers.PLANTRONICS);
filter.addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
filter.addAction(BluetoothHeadset.ACTION_VENDOR_SPECIFIC_HEADSET_EVENT);
mContext.registerReceiver(this, filter);
Log.d("[Bluetooth] Receiver started");
startBluetooth();
}
/**
* Registers a broadcast receiver. The receiver will only be notified of intents that require
* the category given and only for the actions that are supported for that category. If the
* receiver is used by a fragment, create an instance of {@link TTFragmentBroadcastReceiver}
* and register it with the {@link #registerReceiver(TTFragmentBroadcastReceiver)} method
* instead, as it will be easier to maintain.
*
* @param receiver
* The broadcast receiver to be registered.
* @param category
* The category for the actions to be received. Must not be {@code null} and must be a
* supported category.
*
* @throws IllegalArgumentException
* If the category is {@code null}, or is not one of the supported categories.
*/
public static void registerReceiver(BroadcastReceiver receiver, String category) {
final String[] actions = ACTIONS_SUPPORTED_BY_CATEGORY.get(category);
if (category == null || actions.length == 0) {
throw new IllegalArgumentException("Category is not supported: " + category);
}
final IntentFilter filter = new IntentFilter();
filter.addCategory(category);
for (String action : actions) {
// IntentFilter will only match Intents with one of these actions.
filter.addAction(action);
}
LocalBroadcastManager.getInstance(TwistyTimer.getAppContext())
.registerReceiver(receiver, filter);
}
/**
* Constructs an intent filter from user input. This intent-filter is used for cross-profile
* intent.
*
* @return a user constructed intent filter.
*/
private IntentFilter getIntentFilter() {
if (mActions.isEmpty() && mCategories.isEmpty() && mDataSchemes.isEmpty()
&& mDataTypes.isEmpty()) {
return null;
}
IntentFilter intentFilter = new IntentFilter();
for (String action : mActions) {
intentFilter.addAction(action);
}
for (String category : mCategories) {
intentFilter.addCategory(category);
}
for (String dataScheme : mDataSchemes) {
intentFilter.addDataScheme(dataScheme);
}
for (String dataType : mDataTypes) {
try {
intentFilter.addDataType(dataType);
} catch (MalformedMimeTypeException e) {
Log.e(TAG, "Malformed mime type: " + e);
return null;
}
}
return intentFilter;
}
public boolean isMyLauncherDefault() {
final IntentFilter launcherFilter = new IntentFilter(Intent.ACTION_MAIN);
launcherFilter.addCategory(Intent.CATEGORY_HOME);
List<IntentFilter> filters = new ArrayList<IntentFilter>();
filters.add(launcherFilter);
final String myPackageName = getPackageName();
List<ComponentName> activities = new ArrayList<ComponentName>();
packageManager.getPreferredActivities(filters, activities, "com.moonpi.tapunlock");
for (ComponentName activity : activities) {
if (myPackageName.equals(activity.getPackageName())) {
return true;
}
}
return false;
}
@ReactMethod
public void registerReceiver(String action, String category)
{
// THIS METHOD IS DEPRECATED, use registerBroadcastReceiver
Log.d(TAG, "Registering an Intent filter for action: " + action);
this.registeredAction = action;
this.registeredCategory = category;
// User has specified the intent action and category that DataWedge will be reporting
try
{
this.reactContext.unregisterReceiver(scannedDataBroadcastReceiver);
}
catch (IllegalArgumentException e)
{
// Expected behaviour if there was not a previously registered receiver.
}
IntentFilter filter = new IntentFilter();
filter.addAction(action);
if (category != null && category.length() > 0)
filter.addCategory(category);
this.reactContext.registerReceiver(scannedDataBroadcastReceiver, filter);
}
/**
* Ensures intents with {@link CustomTabsService#ACTION_CUSTOM_TABS_CONNECTION} resolve to a
* Service with given categories
*/
public static void installCustomTabsService(String providerPackage, List<String> categories) {
Intent intent = new Intent()
.setAction(CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION)
.setPackage(providerPackage);
IntentFilter filter = new IntentFilter();
for (String category : categories) {
filter.addCategory(category);
}
ResolveInfo resolveInfo = new ResolveInfo();
resolveInfo.filter = filter;
ShadowPackageManager manager = Shadows.shadowOf(RuntimeEnvironment.application
.getPackageManager());
manager.addResolveInfoForIntent(intent, resolveInfo);
}
private void publishRoutes() {
MediaRouteProviderDescriptor.Builder builder = new MediaRouteProviderDescriptor.Builder();
for (CastDevice castDevice : this.castDevices.values()) {
ArrayList<IntentFilter> controlFilters = new ArrayList<IntentFilter>(BASE_CONTROL_FILTERS);
// Include any app-specific control filters that have been requested.
// TODO: Do we need to check with the device?
for (String category : this.customCategories) {
IntentFilter filter = new IntentFilter();
filter.addCategory(category);
controlFilters.add(filter);
}
Bundle extras = new Bundle();
castDevice.putInBundle(extras);
MediaRouteDescriptor route = new MediaRouteDescriptor.Builder(
castDevice.getDeviceId(),
castDevice.getFriendlyName())
.setDescription(castDevice.getModelName())
.addControlFilters(controlFilters)
.setDeviceType(MediaRouter.RouteInfo.DEVICE_TYPE_TV)
.setPlaybackType(MediaRouter.RouteInfo.PLAYBACK_TYPE_REMOTE)
.setVolumeHandling(MediaRouter.RouteInfo.PLAYBACK_VOLUME_FIXED)
.setVolumeMax(20)
.setVolume(0)
.setEnabled(true)
.setExtras(extras)
.setConnectionState(MediaRouter.RouteInfo.CONNECTION_STATE_DISCONNECTED)
.build();
builder.addRoute(route);
}
this.setDescriptor(builder.build());
}
/**
* Executes device management operations on the device.
*
* @param code - Operation object.
*/
public void doTask(String code) {
switch (code) {
case Constants.Operation.GET_APP_DOWNLOAD_PROGRESS:
String downloadingApp = Preference.getString(context, context.getResources().getString(
R.string.current_downloading_app));
JSONObject result = new JSONObject();
ApplicationManager applicationManager = new ApplicationManager(context);
if(applicationManager.isPackageInstalled(Constants.AGENT_PACKAGE_NAME)) {
IntentFilter filter = new IntentFilter(Constants.AGENT_APP_ACTION_RESPONSE);
filter.addCategory(Intent.CATEGORY_DEFAULT);
AgentServiceResponseReceiver receiver = new AgentServiceResponseReceiver();
registerReceiver(receiver, filter);
CommonUtils.callAgentApp(context, Constants.Operation.GET_APP_DOWNLOAD_PROGRESS, null, null);
} else {
try {
result.put(INTENT_KEY_APP, downloadingApp);
result.put(INTENT_KEY_PROGRESS, Preference.getString(context, context.getResources().
getString(R.string.app_download_progress)));
} catch (JSONException e) {
Log.e(TAG, "Result object creation failed" + e);
sendBroadcast(Constants.Status.INTERNAL_SERVER_ERROR, null);
}
sendBroadcast(Constants.Status.SUCCESSFUL, result.toString());
}
break;
default:
Log.e(TAG, "Invalid operation code received");
break;
}
}
private void startMonitorNetStatus() {
if (mNetStatusReceiver != null)
return;
mNetStatusReceiver = new UNetStatusReceiver();
IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
mContext.registerReceiver(mNetStatusReceiver, intentFilter);
mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
/**
* Register a BroadcastReceiver that receives a result from the
* UploadVideoService when a video upload completes.
*/
private void registerReceiver() {
// Create an Intent filter that handles Intents from the
// UploadVideoService.
IntentFilter intentFilter =
new IntentFilter(UploadVideoService.ACTION_UPLOAD_SERVICE_RESPONSE);
intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
// Register the BroadcastReceiver.
LocalBroadcastManager.getInstance(this)
.registerReceiver(mUploadResultReceiver,
intentFilter);
}
private void setUpProviderAPIResultReceiver() {
providerAPIResultReceiver = new ProviderAPIResultReceiver(new Handler(), this);
providerAPIBroadcastReceiver = new ProviderApiSetupBroadcastReceiver(this);
IntentFilter updateIntentFilter = new IntentFilter(BROADCAST_PROVIDER_API_EVENT);
updateIntentFilter.addCategory(Intent.CATEGORY_DEFAULT);
LocalBroadcastManager.getInstance(this).registerReceiver(providerAPIBroadcastReceiver, updateIntentFilter);
}
/**
* Register a BroadcastReceiver that receives a result from the
* UploadVideoService when a video upload completes.
*/
private void registerReceiver() {
// Create an Intent filter that handles Intents from the
// UploadVideoService.
IntentFilter intentFilter =
new IntentFilter(UploadVideoService.ACTION_UPLOAD_SERVICE_RESPONSE);
intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
// Register the BroadcastReceiver.
LocalBroadcastManager.getInstance(this)
.registerReceiver(mUploadResultReceiver,
intentFilter);
}
public void test点击链接() {
final Instrumentation inst = getInstrumentation();
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_VIEW);
intentFilter.addDataScheme("http");
intentFilter.addCategory(Intent.CATEGORY_BROWSABLE);
View link = this.getActivity().findViewById(R.id.link);
ActivityMonitor monitor = inst.addMonitor(
intentFilter, null, false);
assertEquals(0, monitor.getHits());
TouchUtils.clickView(this, link);
monitor.waitForActivityWithTimeout(5000);
assertEquals(1, monitor.getHits());
inst.removeMonitor(monitor);
}
private void registerActionReceiver() {
IntentFilter filter = new IntentFilter(ActionReceiver.PROCESS_RESPONSE);
filter.addCategory(Intent.CATEGORY_DEFAULT);
actionReceiver = new ActionReceiver();
context.registerReceiver(actionReceiver, filter);
}
private void registerBeaconProximityChangeReceiver() {
IntentFilter filter = new IntentFilter(BeaconProximityChangeReceiver.PROCESS_RESPONSE);
filter.addCategory(Intent.CATEGORY_DEFAULT);
beaconProximityChangeReceiver = new BeaconProximityChangeReceiver();
context.registerReceiver(beaconProximityChangeReceiver, filter);
}
public void onCreate() {
IntentFilter filter = new IntentFilter(ACTION_C2DM_REGISTRATION);
filter.addCategory(getPackageName());
registerReceiver(registrationReceiver, filter);
}
IntentFilter getResponseFilter() {
IntentFilter filter = new IntentFilter();
filter.addAction(QueryUtil.createResponseAction(mDataType, mQueryId));
filter.addCategory(QueryUtil.BBQ_CATEGORY);
return filter;
}
private void setDefaultCosuPolicies(boolean active){
// set user restrictions
setUserRestriction(UserManager.DISALLOW_SAFE_BOOT, active);
setUserRestriction(UserManager.DISALLOW_FACTORY_RESET, active);
setUserRestriction(UserManager.DISALLOW_ADD_USER, active);
setUserRestriction(UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA, active);
setUserRestriction(UserManager.DISALLOW_ADJUST_VOLUME, active);
// disable keyguard and status bar
mDevicePolicyManager.setKeyguardDisabled(mAdminComponentName, active);
mDevicePolicyManager.setStatusBarDisabled(mAdminComponentName, active);
// enable STAY_ON_WHILE_PLUGGED_IN
enableStayOnWhilePluggedIn(active);
// set system update policy
if (active){
mDevicePolicyManager.setSystemUpdatePolicy(mAdminComponentName,
SystemUpdatePolicy.createWindowedInstallPolicy(60, 120));
} else {
mDevicePolicyManager.setSystemUpdatePolicy(mAdminComponentName,
null);
}
// set this Activity as a lock task package
mDevicePolicyManager.setLockTaskPackages(mAdminComponentName,
active ? new String[]{getPackageName()} : new String[]{});
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MAIN);
intentFilter.addCategory(Intent.CATEGORY_HOME);
intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
if (active) {
// set Cosu activity as home intent receiver so that it is started
// on reboot
mDevicePolicyManager.addPersistentPreferredActivity(
mAdminComponentName, intentFilter, new ComponentName(
getPackageName(), LockedActivity.class.getName()));
} else {
mDevicePolicyManager.clearPackagePersistentPreferredActivities(
mAdminComponentName, getPackageName());
}
}