下面列出了android.content.IntentFilter#addDataType ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Enables forwarding of share intent between private account and managed profile.
*/
private void enableForwarding() {
Activity activity = getActivity();
if (null == activity || activity.isFinishing()) {
return;
}
DevicePolicyManager manager =
(DevicePolicyManager) activity.getSystemService(Context.DEVICE_POLICY_SERVICE);
try {
IntentFilter filter = new IntentFilter(Intent.ACTION_SEND);
filter.addDataType("text/plain");
filter.addDataType("image/jpeg");
// This is how you can register an IntentFilter as allowed pattern of Intent forwarding
manager.addCrossProfileIntentFilter(BasicDeviceAdminReceiver.getComponentName(activity),
filter, FLAG_MANAGED_CAN_ACCESS_PARENT | FLAG_PARENT_CAN_ACCESS_MANAGED);
} catch (IntentFilter.MalformedMimeTypeException e) {
e.printStackTrace();
}
}
private void setupForegroundDispatch() {
NfcAdapter nfcAdapter = getNfcAdapter();
if (nfcAdapter != null) {
IntentFilter intentFilter = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
try {
intentFilter.addDataType(getNfcMimeType());
} catch (IntentFilter.MalformedMimeTypeException e) {
Log.addEntry(LOG_TAG, e.getMessage());
return;
}
nfcAdapter.enableForegroundDispatch(
this,
PendingIntent.getActivity(
this,
0,
new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0),
new IntentFilter[] {intentFilter},
null);
}
}
/**
* 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;
}
@Test
public void testMimeType1()
{
try
{
Intent i = getIntent();
i.setType("iccta");
IntentFilter f = getIntentFilter();
f.addDataType("iccta/*");
int v = f.match(i.getAction(), i.getType(), i.getScheme(), i.getData(), i.getCategories(), "IccTA");
Assert.assertTrue(v > 0);
}
catch (Exception ex)
{
ex.printStackTrace();
Assert.fail();
}
}
@Test
public void testMimeType2()
{
try
{
Intent i = getIntent();
i.setType("iccta/123");
IntentFilter f = getIntentFilter();
f.addDataType("iccta/*");
int v = f.match(i.getAction(), i.getType(), i.getScheme(), i.getData(), i.getCategories(), "IccTA");
Assert.assertTrue(v > 0);
}
catch (Exception ex)
{
ex.printStackTrace();
Assert.fail();
}
}
/**
* Enables forwarding of share intent between private account and managed profile.
*/
private void enableForwarding() {
Activity activity = getActivity();
if (null == activity || activity.isFinishing()) {
return;
}
DevicePolicyManager manager =
(DevicePolicyManager) activity.getSystemService(Context.DEVICE_POLICY_SERVICE);
try {
IntentFilter filter = new IntentFilter(Intent.ACTION_SEND);
filter.addDataType("text/plain");
filter.addDataType("image/jpeg");
// This is how you can register an IntentFilter as allowed pattern of Intent forwarding
manager.addCrossProfileIntentFilter(BasicDeviceAdminReceiver.getComponentName(activity),
filter, FLAG_MANAGED_CAN_ACCESS_PARENT | FLAG_PARENT_CAN_ACCESS_MANAGED);
} catch (IntentFilter.MalformedMimeTypeException e) {
e.printStackTrace();
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
setContentView(R.layout.main);
findViewById(R.id.write_tag).setOnClickListener(mTagWriter);
// Handle all of our received NFC intents in this activity.
mNfcPendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
// Intent filters for reading a note from a tag or exchanging over p2p.
IntentFilter ndefDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
try {
ndefDetected.addDataType("text/plain");
} catch (MalformedMimeTypeException e) { }
mNdefExchangeFilters = new IntentFilter[] { ndefDetected };
// Intent filters for writing to a tag
IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
mWriteTagFilters = new IntentFilter[] { tagDetected };
}
@Test
public void testMimeType5()
{
try
{
Intent i = getIntent();
i.setType("123/*");
IntentFilter f = getIntentFilter();
f.addDataType("123/iccta");
int v = f.match(i.getAction(), i.getType(), i.getScheme(), i.getData(), i.getCategories(), "IccTA");
Assert.assertTrue(v > 0);
}
catch (Exception ex)
{
ex.printStackTrace();
Assert.fail();
}
}
@Test
public void testMimeType6()
{
try
{
Intent i = getIntent();
i.setType("*/123");
IntentFilter f = getIntentFilter();
f.addDataType("1234/123");
int v = f.match(i.getAction(), i.getType(), i.getScheme(), i.getData(), i.getCategories(), "IccTA");
Assert.assertTrue(v < 0);
}
catch (Exception ex)
{
ex.printStackTrace();
Assert.fail();
}
}
@Test
public void testMimeType7()
{
try
{
Intent i = getIntent();
i.setType("*");
IntentFilter f = getIntentFilter();
f.addDataType("abc/123");
int v = f.match(i.getAction(), i.getType(), i.getScheme(), i.getData(), i.getCategories(), "IccTA");
Assert.assertTrue(v < 0);
}
catch (Exception ex)
{
ex.printStackTrace();
Assert.fail();
}
}
@Override
protected void onResume() {
super.onResume();
Logf.I(TAG, "onResume()");
IntentFilter filter = new IntentFilter(Response.RESPONSE);
try {
filter.addDataType(Encounters.CONTENT_ITEM_TYPE);
filter.addDataType(EncounterTasks.CONTENT_ITEM_TYPE);
} catch (Exception e) {
}
LocalBroadcastManager.getInstance(this.getApplicationContext()).registerReceiver(mReceiver, filter);
//bindSessionService();
// This prevents us from relaunching the login on every resume
dump();
hideViewsByRole();
}
@Override
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
setContentView(R.layout.foreground_dispatch);
mText = (TextView) findViewById(R.id.text);
mText.setText("Scan a tag");
mAdapter = NfcAdapter.getDefaultAdapter(this);
// Create a generic PendingIntent that will be deliver to this activity. The NFC stack
// will fill in the intent with the details of the discovered tag before delivering to
// this activity.
mPendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
// Setup an intent filter for all MIME based dispatches
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
try {
ndef.addDataType("*/*");
} catch (MalformedMimeTypeException e) {
throw new RuntimeException("fail", e);
}
mFilters = new IntentFilter[] {
ndef,
};
// Setup a tech list for all NfcF tags
mTechLists = new String[][] { new String[] { NfcF.class.getName() } };
}
public void init() {
Intent nfcIntent = new Intent(this, this.getClass());
nfcIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, nfcIntent, 0);
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
try {
ndef.addDataType("*/*"); /* Handles all MIME based dispatches.
You should specify only the ones that you need. */
} catch (IntentFilter.MalformedMimeTypeException e) {
throw new RuntimeException("fail", e);
}
IntentFilter[] intentFiltersArray = new IntentFilter[]{ndef,};
String[][] techList = new String[1][1];
techList[0][0] = MifareClassic.class.getName();
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
nfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techList);
disposable.add(
nfcManager.requestProgressProcessor().onBackpressureBuffer()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
message -> binding.currentMessage.setText(message),
Timber::e
)
);
disposable.add(
nfcManager.requestInitProcessor()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(init -> {
if (!init) {
binding.nfcBg.animate().scaleX(0.1f).scaleY(0.1f).setDuration(1500)
.withEndAction(() -> binding.nfcBg.setVisibility(View.GONE));
}
},
Timber::e)
);
}
public void initializeNFC() {
if (nfcInit == false) {
PackageManager pm = getPackageManager();
nfcSupported = pm.hasSystemFeature(PackageManager.FEATURE_NFC);
if (nfcSupported == false) {
return;
}
// when is in foreground
MLog.d(TAG, "starting NFC");
mAdapter = NfcAdapter.getDefaultAdapter(this);
// PedingIntent will be delivered to this activity
mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
// Setup an intent filter for all MIME based dispatches
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
try {
ndef.addDataType("*/*");
} catch (IntentFilter.MalformedMimeTypeException e) {
throw new RuntimeException("fail", e);
}
mFilters = new IntentFilter[]{ndef,};
// Setup a tech list for all NfcF tags
mTechLists = new String[][]{new String[]{NfcF.class.getName()}};
nfcInit = true;
}
}
public IntentFilter buildFilter() {
Log.i(TAG, "buildFilter()");
IntentFilter filter = new IntentFilter(Response.RESPONSE);
filter.addDataScheme(Subjects.CONTENT_URI.getScheme());
try {
filter.addDataType(Subjects.CONTENT_TYPE);
filter.addDataType(Subjects.CONTENT_ITEM_TYPE);
} catch (IntentFilter.MalformedMimeTypeException e) {
}
return filter;
}
private static void addDataTypeUnchecked(IntentFilter filter, String type) {
try {
filter.addDataType(type);
} catch (MalformedMimeTypeException ex) {
throw new RuntimeException(ex);
}
}
private static void addDataTypeUnchecked(IntentFilter filter, String type) {
try {
filter.addDataType(type);
} catch (MalformedMimeTypeException ex) {
throw new RuntimeException(ex);
}
}
@Override
public IntentFilter buildFilter() {
Log.i(TAG, "buildFilter()");
IntentFilter filter = new IntentFilter(Response.RESPONSE);
filter.addDataScheme(Subjects.CONTENT_URI.getScheme());
try {
filter.addDataType(Subjects.CONTENT_ITEM_TYPE);
filter.addDataType(Subjects.CONTENT_TYPE);
} catch (Exception e) {
}
return filter;
}
public IntentFilter buildFilter() {
Log.i(TAG, "buildFilter()");
IntentFilter filter = new IntentFilter(DispatchResponseReceiver.BROADCAST_RESPONSE);
filter.addDataScheme(Encounters.CONTENT_URI.getScheme());
try {
filter.addDataType(Encounters.CONTENT_ITEM_TYPE);
} catch (MalformedMimeTypeException e) {
}
return filter;
}
public IntentFilter buildFilter() {
IntentFilter filter = new IntentFilter(Response.RESPONSE);
try {
filter.addDataType(EncounterTasks.CONTENT_TYPE);
filter.addDataType(EncounterTasks.CONTENT_ITEM_TYPE);
filter.addDataType(Subjects.CONTENT_TYPE);
} catch (MalformedMimeTypeException e) {
}
return filter;
}