Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude a specific application from ACTION_VIEW Intent?

I am trying to load a twitter URL in browser.

In my phone, I have already installed twitter app also. I am trying to open URL using ACTION_VIEW intent. But what happens is, when I call intent, android will show default chooser dialog, which contains twitter app also (if it is installed on device). I want to open the URL using browsers only. I want to exclude Twitter application from the dialog. I want all availaible browsers in device to show up in dialog, not native applications like twitter, facebook etc.

Is it is possible or not? If possible could anyone help me in achieving it. I am also attaching my code and a screenshot along with this question for clarity.

String url = "https://twitter.com";
MimeTypeMap map = MimeTypeMap.getSingleton();
String type = map.getMimeTypeFromExtension(url);
Intent i = new Intent(Intent.ACTION_VIEW);
i.setType(type);
i.setData(Uri.parse(url));
startActivity(i);

enter image description here

like image 593
Sanal Varghese Avatar asked Sep 08 '13 10:09

Sanal Varghese


People also ask

What is the use of intent createChooser () method?

createChooser(intent, title); // Try to invoke the intent. // Define what your app should do if no activity can handle the intent. This displays a dialog with a list of apps that respond to the intent passed to the createChooser() method and uses the supplied text as the dialog title.

What is category in intent filter?

Categories are used for implicit Intents. So, If your Activity can be started by an implicit Intent when no other specific category is assigned to activity, activity's Intent filter should include this category. (even if you have other categories in the Intent filter).

Is intent deprecated?

The Intent is not deprecated the problem is with your itemClickable. class because it is not recognized. Either you have somehow enabled extra warnings that identify Intent as deprecated or something is really stack at your machine.

What is intent Flag_activity_new_task?

The flags you can use to modify the default behavior are: FLAG_ACTIVITY_NEW_TASK. Start the activity in a new task. If a task is already running for the activity you are now starting, that task is brought to the foreground with its last state restored and the activity receives the new intent in onNewIntent() .


2 Answers

I needed to do a similar thing and found this answer helpful. I've modified it so that it is a complete solution:

public static void openFileWithInstalledAppExceptCurrentApp(File file, final Activity activity) {
    Intent intent = new Intent();
    intent.setAction(android.content.Intent.ACTION_VIEW);
    MimeTypeMap mime = MimeTypeMap.getSingleton();
    String ext = file.getName().substring(file.getName().indexOf(".")+1);
    String type = mime.getMimeTypeFromExtension(ext);
    intent.setDataAndType(Uri.fromFile(file),type);
    PackageManager packageManager = activity.getPackageManager();
    List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
    String packageNameToHide = "com.test.app";
    ArrayList<Intent> targetIntents = new ArrayList<Intent>();
    for (ResolveInfo currentInfo : activities) {
            String packageName = currentInfo.activityInfo.packageName;
        if (!packageNameToHide.equals(packageName)) {
            Intent targetIntent = new Intent(android.content.Intent.ACTION_VIEW);
            targetIntent.setDataAndType(Uri.fromFile(file),type);
            targetIntent.setPackage(packageName);
            targetIntents.add(targetIntent);
        }
    }
    if(targetIntents.size() > 0) {
        Intent chooserIntent = Intent.createChooser(targetIntents.remove(0), "Open file with");
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetIntents.toArray(new Parcelable[] {}));
        activity.startActivity(chooserIntent);
    }
    else {
        Toast.makeText(this, "No app found", Toast.LENGTH_SHORT).show();
    }
}
like image 68
Sufian Avatar answered Oct 23 '22 16:10

Sufian


You just need to set the target package to the intent:

String url = "https://twitter.com";
MimeTypeMap map = MimeTypeMap.getSingleton();
String type = map.getMimeTypeFromExtension(url);
Intent i = new Intent(Intent.ACTION_VIEW);
i.setType(type);
i.setData(Uri.parse(url));
ComponentName comp = new ComponentName("com.android.browser", "com.android.browser.BrowserActivity");
i.setComponent(comp);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

However, users my be disturbed if they have some custom browser installed and want to use this as default.

You can try to detect the default browser with:

Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://example.com"));
List<ResolveInfo> list = context.getPackageManager()
    .queryIntentActivities(i, 0);
// walk through list and select your most favorite browser
like image 4
flx Avatar answered Oct 23 '22 16:10

flx