我正在关注Udemy.com的一些教程,我收到此语法错误“无法解析方法”isIntentAvailable(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();
}
}
}
});
答案 0 :(得分:1)
添加此方法:
private boolean isIntentAvailable(Intent intent) {
return getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY).size() > 0;
}
答案 1 :(得分: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;
}
答案 2 :(得分:0)
使用标准库中的方法可能更好 - https://developer.android.com/reference/android/content/pm/PackageManager.html#resolveActivity(android.content.Intent,int)。它还具有 resolveService 和 resolveContentProvider 方法。
答案 3 :(得分:0)
在[android blog](https://android-developers.googleblog.com/2009/01/can-i-use-this-intent.html)中讨论了更多信息
/**
* 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);
}