For sending the value we will use intent. putExtra("key", Value); and during receive intent on another activity we will use intent. getStringExtra("key"); to get the intent data as String or use some other available method to get other types of data ( Integer , Boolean , etc.).
Use startActivityForResult in your calling activity to start the activity: ActivityCompat. startActivityForResult(this, new Intent(this, MyActivity. class), 0, null);
The Intent describes the activity to start and carries any necessary data. If you want to receive a result from the activity when it finishes, call startActivityForResult() . Your activity receives the result as a separate Intent object in your activity's onActivityResult() callback.
Activity is a UI component which you see on your screen. An Intent is a message object which is used to request an action from the same/different app component.
edwardxu's solution works perfectly for me.
Just to clarify a bit:
PackageManager packageManager = getActivity().getPackageManager();
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent);
} else {
Log.d(TAG, "No Intent available to handle action");
}
PackageManager manager = context.getPackageManager();
List<ResolveInfo> infos = manager.queryIntentActivities(intent, 0);
if (infos.size() > 0) {
//Then there is an Application(s) can handle your intent
} else {
//No Application can handle your intent
}
Have you tried this intent?
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(yourFileHere));
if (intent.resolveActivity(getPackageManager()) == null) {
// No Activity found that can handle this intent.
}
else{
// There is an activity which can handle this intent.
}
You can use:
public static boolean isAvailable(Context ctx, Intent intent) {
final PackageManager mgr = ctx.getPackageManager();
List<ResolveInfo> list =
mgr.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With