在 Android ICS 中,我们可以禁用内置应用程序。是否可以知道特定应用程序是否在代码中被禁用或启用?
问问题
3247 次
3 回答
15
ApplicationInfo ai = getActivity().getPackageManager().getApplicationInfo("your_package",0);
boolean appStatus = ai.enabled;
如果appStatus
是,false
则应用程序被禁用:)
编辑:
在 Android 11(API 级别 30)上,您还需要告诉清单您要从哪些应用收集信息:
<manifest package="com.example.game">
<queries>
<package android:name="com.example.store" />
</queries>
</manifest>
来源:https ://developer.android.com/training/basics/intents/package-visibility#package-name
于 2013-01-28T08:22:59.400 回答
5
接受的答案也可能导致异常,即,NameNotFoundException
因此您可能必须构建一个静默捕获异常并确定启用状态的流程(它实际上是第三种状态,即未安装)。
因此,最好像这样找到已启用和已安装的状态:
public static final int ENABLED = 0x00;
public static final int DISABLED = 0x01;
public static final int NOT_INSTALLED = 0x03;
/**
* @param context Context
* @param packageName The Package name of the concerned App
* @return State of the Application.
*
*/
public static int getAppState(@NonNull Context context, @NonNull String packageName) {
final PackageManager packageManager = context.getPackageManager();
// Check if the App is installed or not first
Intent intent = packageManager.getLaunchIntentForPackage(packageName);
if (intent == null) {
return NOT_INSTALLED;
}
List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
if(list.isEmpty()){
// App is not installed
return NOT_INSTALLED;
}
else{
// Check if the App is enabled/disabled
int appEnabledSetting = packageManager.getApplicationEnabledSetting(packageName);
if(appEnabledSetting == COMPONENT_ENABLED_STATE_DISABLED ||
appEnabledSetting == COMPONENT_ENABLED_STATE_DISABLED_USER){
return DISABLED;
}
else{
return ENABLED;
}
}
}
于 2018-02-19T08:40:26.067 回答
0
尝试http://developer.android.com/reference/android/content/pm/PackageManager.html#getApplicationInfo%28java.lang.String,%20int%29并检查返回的 ApplicationInfo 对象中的“启用”标志。
于 2012-07-18T09:43:47.710 回答