【问题标题】:Limiting Android PackageManager to a single choice将 Android PackageManager 限制为单一选择
【发布时间】:2012-12-28 22:44:09
【问题描述】:

当一个文件被选中时,我的应用程序会调用 android PackageManager,并为用户提供一个应用程序选项来处理文件的处理方式。我想将此选择限制为蓝牙。目前蓝牙是第一个选项,这很好,这一切都有效。我想知道是否可以只向用户展示这个选项。

    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"));

                    }

                }
            }
        }

【问题讨论】:

  • ACTION_SEND 背后的意义在于让用户控制数据的发送地点和方式。
  • 是的,我明白这一点,但我的问题是寻求替代方案。
  • 最可能的选择是不使用ACTION_SEND。毕竟,“蓝牙”不会出现在所有设备上,并且在它所在的那些设备上可能没有相同的功能。所以,如果你真的想通过蓝牙对你的数据做一些事情,那就自己做吧。
  • 谢谢,但这是一个蓝牙应用程序。它不适用于没有蓝牙的设备。
  • 我指的不是蓝牙技术。我指的是“蓝牙”,您在选择器中看到的条目。在任何给定的 Android 设备上都不一定有“蓝牙”活动会显示在选择器中,并且“蓝牙”活动不一定在每台设备上都做同样的事情(更不用说你认为它做了什么或做什么)用户希望它这样做)。要么允许您的用户选择“发送”数据的位置,要么自己编写蓝牙处理代码以确保它按照您的预期进行。

标签: android bluetooth


【解决方案1】:

解决方案:找出设备对你的意图支持哪些应用,找到那个是蓝牙的,直接调用它。

这篇文章回答了你的问题: http://tsicilian.wordpress.com/2012/11/06/bluetooth-data-transfer-with-android/

来自文章: 我们可以看到 BT 应用程序在这些处理程序中。我们当然可以让用户从列表中选择该应用程序并完成它。但是如果我们觉得我们应该对用户更友好一点,我们需要更进一步并自己启动应用程序,而不是简单地在其他不必要的选项中显示它……但是如何?

一种方法是这样使用 Android 的 PackageManager:

//list of apps that can handle our intent
PackageManager pm = getPackageManager();
List appsList = pm.queryIntentActivities( intent, 0);

if(appsList.size() > 0 {
   // proceed
}

上面的 PackageManager 方法以封装我们需要的信息的 ResolveInfo 对象列表的形式返回我们之前看到的所有容易处理我们的文件传输意图的活动的列表:

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

我们现在有了自己开始 BT 所需的信息:

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

我们所做的是使用之前检索到的包及其对应的类。由于我们是一群好奇的人,我们可能想知道“com.android.bluetooth”包的类名是什么。如果我们将它打印出来,我们会得到:com.broadcom.bt.app.opp.OppLauncherActivity。 OPP 代表 Object Push Profile,是允许无线共享文件的 Android 组件。

本文还介绍了如何从您的应用程序启用蓝牙。

【讨论】:

  • 不鼓励只发布到其他网站的链接,因为它们与问题没有直接关系,如果链接失效,这个答案就变得毫无价值。除了链接之外,您还应该至少发布与问题相关的部分以及解释。
猜你喜欢
  • 1970-01-01
  • 2011-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-12
  • 1970-01-01
  • 2012-06-27
  • 1970-01-01
相关资源
最近更新 更多