如何检查是否有意向可以从一些活动来处理?有意向

2023-09-12 07:35:31 作者:皇上,您这是喜脉

我有这样的方法,到目前为止,但它想出了像缺了点什么

例如我有一个文件/sdcard/sound.3ga返回假(如没有活动,可以处理这种类型的文件),但是当我从文件管理器打开它的媒体播放器没有打开问题

我觉得这个意图是不完整的,我需要更多的东西,使我的自我确保handlerExists变量将是假的仅当有没有活动,可以处理这个意图

  PackageManager PM = getPackageManager();
        意向意图=新的意图(android.content.Intent.ACTION_VIEW);
        字符串延长= android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(新文件(uriString中))的toString());
        字符串MIMETYPE = android.webkit.MimeTypeMap.getSingleton()getMimeTypeFromExtension(扩展)。
        intent.setDataAndType(Uri.fromFile(新文件(uriString中)),MIMETYPE);
        布尔handlerExists = intent.resolveActivity(PM)!= NULL;
 

解决方案

  PackageManager经理= context.getPackageManager();
    名单< ResolveInfo>相关信息= manager.queryIntentActivities(意向,0);
        如果(infos.size()大于0){
             //再有就是应用程序可以处理你的意图
        }其他{
             //没有应用程序可以处理你的意图
        }
 
国庆能否出省 是否做核酸检测 福州市教育局这样回应

// 你有没有试过这种意图是什么?

 意向意图=新的意图(Intent.ACTION_VIEW,Uri.fromFile(yourFileHere));
 

I have this method so far , but it came up like something is missing

for example I have a file /sdcard/sound.3ga that returns false ( like there is no activity that can handle this type of file ) , But when I open it from the file manager it opens with the media player with no problem

I think this intent is not complete and I need to to something more to make my self sure that the handlerExists variable will be false ONLY if there is no activity that can handle this intent

PackageManager pm = getPackageManager();
        Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
        String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(new File(uriString)).toString());
        String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        intent.setDataAndType(Uri.fromFile(new File(uriString)),mimetype);
        boolean handlerExists = intent.resolveActivity(pm) != null;

解决方案

    PackageManager manager = context.getPackageManager();
    List<ResolveInfo> infos = manager.queryIntentActivities(intent, 0);
        if (infos.size() > 0) {
             //Then there is application 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));

 
精彩推荐
图片推荐