Java 无法解析方法';iIntentAvailable(android.content.intent)和#x27;

Java 无法解析方法';iIntentAvailable(android.content.intent)和#x27;,java,android,android-intent,Java,Android,Android Intent,我正在Udemy.com上学习一些教程,我发现这个语法错误“无法解析方法'IsIntentavable(android.content.intent)' 我试图在网上查找,但找不到解决方案。它是不推荐的还是什么 提前谢谢 mPerformButton = (Button) findViewById(R.id.performImplicit); mPerformButton.setOnClickListener(new View.OnClickListener() {

我正在Udemy.com上学习一些教程,我发现这个语法错误“无法解析方法'IsIntentavable(android.content.intent)' 我试图在网上查找,但找不到解决方案。它是不推荐的还是什么

提前谢谢

   mPerformButton = (Button) findViewById(R.id.performImplicit);
    mPerformButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int position = mSpinner.getSelectedItemPosition();
            Intent implicitIntent = null;
            switch (position){

                case 0 :
                    //nothing selected
                    break;
                case 1:
                    //deltaprogram.us
                    implicitIntent = new Intent(Intent.ACTION_VIEW,
                            Uri.parse("http://deltaprogram.us"));
                    break;
                case 2:
                    //call someone
                    implicitIntent = new Intent(Intent.ACTION_DIAL,
                            Uri.parse("tel:(+000)8675309"));
                    break;
                case 3:
                    //map of YETspace
                    implicitIntent = new Intent(Intent.ACTION_VIEW,
                            Uri.parse("geo:30.2715,-97.742"));
                    break;
                case 4:
                    //take a picture( not returning it here though)
                    implicitIntent = new Intent("android.media.action.IMAGE_CAPTURE");
                    break;
                case 5:
                    //edit first contact
                    implicitIntent = new Intent(Intent.ACTION_EDIT,
                            Uri.parse("content://contacts/people/1"));
                    break;

            }

            if(implicitIntent != null){

                if(isIntentAvailable(implicitIntent) == true){ //Syntax Problem here
                    startActivity(implicitIntent);
                }else{
                    Toast.makeText(v.getContext(),"no application available", Toast.LENGTH_LONG).show();
                }
            }

        }
    });
添加此方法:

private boolean isIntentAvailable(Intent intent) {
    return getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY).size() > 0;
}

添加函数定义:

/**
 * Indicates whether the specified action can be used as an intent. This
 * method queries the package manager for installed packages that can
 * respond to an intent with the specified action. If no suitable package is
 * found, this method returns false.
 *
 * @param context The application's environment.
 * @param action The Intent action to check for availability.
 *
 * @return True if an Intent with the specified action can be sent and
 *         responded to, false otherwise.
 */
public static boolean isIntentAvailable(Context context, String action) {
    final PackageManager packageManager = context.getPackageManager();
    final Intent intent = new Intent(action);
    List<ResolveInfo> list =
            packageManager.queryIntentActivities(intent,
                    PackageManager.MATCH_DEFAULT_ONLY);
    return list.size() > 0;
}
/**
*指示指定的操作是否可以用作意图。此
*方法查询包管理器以查找可以
*使用指定的操作响应意图。如果没有合适的包
*如果找到,则此方法返回false。
*
*@param context应用程序的环境。
*@param action检查可用性的意向操作。
*
*@如果可以发送具有指定操作的意图,则返回True
*回答为,否则为假。
*/
公共静态布尔值isIntentAvailable(上下文、字符串操作){
final-PackageManager-PackageManager=context.getPackageManager();
最终意图=新意图(行动);
列表=
packageManager.QueryInputActivities(意图,
PackageManager.MATCH_(仅限默认值);
返回列表.size()>0;
}

也许使用标准库中的方法更好。它还有resolveServiceresolveContentProvider方法。

更多信息,请参见[android博客]()

/**
 * Indicates whether the specified action can be used as an intent. This
 * method queries the package manager for installed packages that can
 * respond to an intent with the specified action. If no suitable package is
 * found, this method returns false.
 *
 * @param context The application's environment.
 * @param action The Intent action to check for availability.
 *
 * @return True if an Intent with the specified action can be sent and
 *         responded to, false otherwise.
 */
public static boolean isIntentAvailable(Context context, String action) {
    final PackageManager packageManager = context.getPackageManager();
    final Intent intent = new Intent(action);
    List<ResolveInfo> list =
            packageManager.queryIntentActivities(intent,
                    PackageManager.MATCH_DEFAULT_ONLY);
    return list.size() > 0;
}

这是一个你需要自己定义的方法……我在谷歌搜索时也看到了
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    final boolean scanAvailable = isIntentAvailable(this,
        "com.google.zxing.client.android.SCAN");

    MenuItem item;
    item = menu.findItem(R.id.menu_item_add);
    item.setEnabled(scanAvailable);

    return super.onPrepareOptionsMenu(menu);
}