Limit Android PackageManager to one choice

My application calls the Android PackageManager when a file is selected, and the user is given a choice of applications to handle how the file should be processed. I want to limit this choice to Bluetooth. Bluetooth is currently appearing as the first option, which is good, and it all works. I was wondering if it is only possible to provide the user with this single option.

case REQUEST_FILE_SELECT: if (requestCode == REQUEST_FILE_SELECT) { // Get the Uri of the selected file Uri uri = data.getData(); Log.d(TAG, "File Uri: " + uri.toString()); // Get the path String path = null; try { path = FileUtils.getPath(this, uri); } catch (URISyntaxException e) { e.printStackTrace(); } Log.d(TAG, "File Path: " + path); // Get the file instance File mFile = new File(path); // Evoke the file chooser List<Intent> targetedShareIntents = new ArrayList<Intent>(); Intent shareIntent = new Intent( android.content.Intent.ACTION_SEND); shareIntent.setType("*/*"); // Evoke the package manager List<ResolveInfo> resInfo = getPackageManager() .queryIntentActivities(shareIntent, PackageManager.GET_ACTIVITIES); if (!resInfo.isEmpty()) { for (ResolveInfo resolveInfo : resInfo) { String packageName = resolveInfo.activityInfo.packageName; if (packageName.equals("com.android.bluetooth")) { Intent targetedShareIntent = new Intent( android.content.Intent.ACTION_SEND); shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(mFile)); targetedShareIntent.setPackage(packageName); targetedShareIntents.add(targetedShareIntent); startActivity(Intent.createChooser(shareIntent, "Share File")); } } } } 
+1
source share
1 answer

Solution: find out which applications support the device for your intention, find the one that is bluetooh, call it directly.

This article answers your question: http://tsicilian.wordpress.com/2012/11/06/bluetooth-data-transfer-with-android/

From the article: We see that the BT application is one of those handlers. We could, of course, let the user select this application from the list and do with it. But if we feel that we should be more convenient for the user, we need to go further and run the application ourselves, and not just display it in the environment of other unnecessary options ... But how?

One way to do this is to use the Androids PackageManager as follows:

 //list of apps that can handle our intent PackageManager pm = getPackageManager(); List appsList = pm.queryIntentActivities( intent, 0); if(appsList.size() > 0 { // proceed } 

The aforementioned PackageManager method returns a list that we saw earlier of all actions subject to processing file transfer intentions, in the form of a list of ResolveInfo objects that encapsulate the necessary information:

 //select bluetooth String packageName = null; String className = null; boolean found = false; for(ResolveInfo info: appsList){ packageName = info.activityInfo.packageName; if( packageName.equals("com.android.bluetooth")){ className = info.activityInfo.name; found = true; break;// found } } if(! found){ Toast.makeText(this, R.string.blu_notfound_inlist, Toast.LENGTH_SHORT).show(); // exit } 

Now we have the necessary information to start BT:

 //set our intent to launch Bluetooth intent.setClassName(packageName, className); startActivity(intent); 

What we have done is to use the package and the corresponding class obtained earlier. Since we are a curious bunch, we may wonder what the class name is for the package "com.android.bluetooth". This is what we would get if we printed it: com.broadcom.bt.app.opp.OppLauncherActivity. OPP stands for Object Push Profile and an Android component that allows you to transfer files wirelessly.

Also in the article how to enable Bluetooth from your application.

+1
source

All Articles