下面列出了怎么用android.content.IntentFilter.MalformedMimeTypeException的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 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;
}
/** 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 };
}
/**
* @return Registration id to use in unregister(regId) method
*/
public static synchronized int register(String receiverId, EventsListener listener) {
check();
if (listener == null) throw new NullPointerException("Listener cannot be null");
EventsReceiver receiver = new EventsReceiver();
receiver.mReceiverId = receiverId;
receiver.mListener = listener;
try {
sAppContext.registerReceiver(receiver, new IntentFilter(sIntentAction, BASE_MIME_TYPE + "/*"));
} catch (MalformedMimeTypeException e) {
e.printStackTrace();
}
sReceiversMap.put(++sRegistrationId, receiver);
for (Intent sticky : sStickyEventsMap.values()) {
receiver.onReceive(sAppContext, sticky);
}
return sRegistrationId;
}
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);
}
}
private static void addDataTypeUnchecked(IntentFilter filter, String type) {
try {
filter.addDataType(type);
} catch (MalformedMimeTypeException ex) {
throw new RuntimeException(ex);
}
}
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;
}
public static IntentFilter[] getIntentFilterArray() {
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
try {
ndef.addDataType(SONY_MIME_TYPE);
} catch (MalformedMimeTypeException e) {
throw new RuntimeException("fail", e);
}
return new IntentFilter[]{ndef};
}
@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() } };
}