Here are several things you could try:
1. Make sure the application that you want to start has implemented the right intent.
<application android:label="@string/app_name">
...
<activity android:name=".PleaseStartThisActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
</intent-filter>
</activity>
...
</application>
2. Always use a new intent and never reuse the old one.
Intent intent = new Intent(android.content.Intent.ACTION_VIEW); //OK.
// Intent intent = getIntent(); //Not OK.
3. Set everything else right.
// If package name and activity are known.
intent.setComponent(new ComponentName("org.irmacard.cardemu", "org.irmacard.cardemu.PleaseStartThisActivity"));
// Else
// intent = new Intent(android.content.Intent.ACTION_GET_CONTENT);
// intent.setDataAndType(Uri.parse("file://" + filePath), "text/plain");
4. Set or add flags might help
intent.setFlags(
android.content.Intent.FLAG_ACTIVITY_FORWARD_RESULT |
android.content.Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP |
android.content.Intent.FLAG_INCLUDE_STOPPED_PACKAGES |
android.content.Intent.FLAG_RECEIVER_FOREGROUND |
android.content.Intent.FLAG_ACTIVITY_NO_ANIMATION |
android.content.Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION |
android.content.Intent.FLAG_GRANT_PREFIX_URI_PERMISSION |
android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION |
android.content.Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
5. Now you can safely start the activity.
startActivity(intent);
// Or this:
// startActivity(android.content.Intent.createChooser(intent, null));
EDIT
* intent.getDataString() - To get intent data (You may have to parse it to get the package name).
* getPackageManager().queryIntentActivities() - To check available activities for that package like the following example:
private final String getActivity(final String packageName, final Intent intent, final Bundle bundle, final String mimeType) {
final String activity;
int activityLen = 0;
final int packageLen = packageName.length();
if(bundle != null && (activity = bundle.getString("activity")) != null) {
activityLen = activity.length();
} else {
activity = null;
}
final List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent, PackageManager.GET_RESOLVED_FILTER);
for(ResolveInfo ri : list) {
final IntentFilter filter = ri.filter;
final String activityName = ri.activityInfo.name;
//Make sure the activity is valid and exist for the package.
//Tolerate with case insensitive
if(activity != null && activity.regionMatches(true, 0, activityName, 0, activityLen)
&& packageName.regionMatches(true, 0, ri.activityInfo.packageName, 0, packageLen)) {
return activityName;
} else if(filter == null) {
Toast.makeText(this, "ERROR: No available ACTIONS to handle this file", 1).show();
return null;
//Auto search only if user hasn't specified the activity, else return null. Tolerate with case insensitive
} else if(activity == null && filter.hasAction(intent.getAction()) && filter.hasDataType(mimeType)) {
if(ri.activityInfo.packageName.regionMatches(true, 0, packageName, 0, packageLen)) {
return ri.activityInfo.name;
}
} // Else keep validating next activities
}
if(activity == null) {
Toast.makeText(this, "ERROR: No available ACTIVITIES to open this file", 1).show();
} else {
Toast.makeText(this, "ERROR: No such activity found", 1).show();
}
return null;
}