Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if an intent can be handled from some activity?

People also ask

How can I get Intent data from another activity?

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.).

How do you check which Intent started the activity?

Use startActivityForResult in your calling activity to start the activity: ActivityCompat. startActivityForResult(this, new Intent(this, MyActivity. class), 0, null);

What is Intent How do you call Intent from an activity?

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.

What is the difference between activity and Intent?

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;
}