如何检查某个活动是否可以处理意图?

时间:2013-03-14 11:07:09

标签: android android-intent mime-types file-type intentfilter

到目前为止,我有这种方法,但它似乎缺少了一些东西

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

我认为这个意图并不完整,我需要更多的东西来让我自己确定只有没有可以处理此意图的活动时,handlerExists变量才会为false

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;

8 个答案:

答案 0 :(得分:115)

爱德华兹的解决方案非常适合我。

只是澄清一下:

PackageManager packageManager = getActivity().getPackageManager();
if (intent.resolveActivity(packageManager) != null) {
    startActivity(intent);
} else {
    Log.d(TAG, "No Intent available to handle action");
}

答案 1 :(得分:76)

PackageManager manager = context.getPackageManager();
List<ResolveInfo> infos = manager.queryIntentActivities(intent, 0);
if (infos.size() > 0) {
    //Then there is an Application(s) can handle your intent
} else {
    //No Application can handle your intent
}

你试过这个意图吗?

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(yourFileHere));

答案 2 :(得分:40)

if (intent.resolveActivity(getPackageManager()) == null) {
    // No Activity found that can handle this intent. 
}
else{
    // There is an activity which can handle this intent. 
}

答案 3 :(得分:2)

您可以使用:

public class Force {

    private int id1;
    private String subcontractors;
    private String noOfPerson;
    private String noOfHours;

    public void setID(int  id1)
    {
        this.id1=id1;
    }

    public int getID()
    {
        return this.id1;
    }

    public void setSubcontractors(String subcontractors)
    {
        this.subcontractors=subcontractors;
    }

    public String getSubcontractors()
    {
        return this.subcontractors;
    }

    public void setNoOfPerson(String noOfPerson)
    {
        this.noOfPerson=noOfPerson;
    }

    public String getNoOfPerson()
    {
        return this.noOfPerson;
    }

    public void setNoOfHours(String noOfHours)
    {
        this.noOfHours=noOfHours;
    }

    public String getNoOfHours()
    {
        return this.noOfHours;
    }



}

答案 4 :(得分:1)

使用Kotlin如果您在意图不可用时需要做些事情,

fun isIntentAvailable(context: Context, action: String?): Boolean {
    val packageManager = context.packageManager
    val intent = Intent(action)
    val resolveInfo: List<*> = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
    return resolveInfo.isNotEmpty()
}

将该方法实施为

private const val SAMPLE_INTENT = "com.realwear.barcodereader.intent.action.SCAN_BARCODE"

if(isIntentAvailable(this,SAMPLE_INTENT)){
    //Do Stuff
}

如果您无事可做,

Intent(SAMPLE_INTENT).also { barcodeReaderIntent ->
    barcodeReaderIntent.resolveActivity(packageManager)?.also {
        barcodeReaderIntent.putExtra(EXTRA_CODE_128, false)
        startActivityForResult(barcodeReaderIntent, BARCODE_REQUEST_CODE)
    }
}

答案 5 :(得分:1)

解决此问题的明确答案

if (intent.resolveActivity(getPackageManager()) != null) {
    startActivity(intent);
}

答案 6 :(得分:0)

使用kotlin扩展名的另一种方法

 fun Context.isIntentAvailable(intent: Intent): Boolean {
    val resolveInfo: List<*> = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
    return resolveInfo.isNotEmpty()
 }

片段内部的用法

val available = requireContext().isIntentAvailable(this)

内部活动的使用

val available = this.isIntentAvailable(this)

答案 7 :(得分:0)

一个在 2021 年寻找类似内容的小更新 :) 由于 Android 11 不需要调用包管理器,因此需要额外的努力。 那么为什么不将 startActivity() 包装在 try-catch 中呢?甚至更好 - 使用 Rx 漂亮的错误处理:

....
.flatMapCompletable { 
      doSomethingThrowable()
          .onErrorResumeNext { completableCallbackIfNotResolvable() }
}
.subscribe()