gpt4 book ai didi

android - 将 Android PackageManager 限制为单一选择

转载 作者:行者123 更新时间:2023-11-30 03:51:41 26 4
gpt4 key购买 nike

当选择一个文件时,我的应用程序会调用 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"));

}

}
}
}

最佳答案

解决方法:查看设备支持哪些app,找到那个是蓝牙的,直接调用。

本文回答了您的问题: 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 对象列表的形式返回我们之前看到的所有易受处理我们的文件传输 Intent 的 Activity 的列表:

//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 组件。

同样在文章中,如何从您的应用程序启用蓝牙。

关于android - 将 Android PackageManager 限制为单一选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14061470/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com