import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.widget.TextView;
public class AppTypeActivity extends Activity {
//软件类型判断软件
//未知软件类型
public static final int UNKNOW_APP = 0;
//用户软件类型
public static final int USER_APP = 1;
//系统软件
public static final int SYSTEM_APP = 2;
//系统升级软件
public static final int SYSTEM_UPDATE_APP = 4;
//系统+升级软件
public static final int SYSTEM_REF_APP = SYSTEM_APP | SYSTEM_UPDATE_APP;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//要判断的包名
String pname = "com.hiapk.marketpho";
//在text输出的文字
StringBuffer sb = new StringBuffer();
sb.append(pname).append(" is: ");
switch (checkAppType(pname)) {
case SYSTEM_REF_APP:
//是系统rom集成软件
sb.append("SYSTEM_REF_APP");
break;
case USER_APP:
//是用户安装软件
sb.append("USER_APP");
break;
default:
//未知软件
sb.append("UNKNOW_APP");
break;
}
TextView tv = (TextView)findViewById(R.id.textView);
tv.setText(sb.toString());
}
/**
* 检查app是否是系统rom集成的
* @param pname
* @return
*/
private int checkAppType(String pname) {
try {
PackageInfo pInfo = getPackageManager().getPackageInfo(pname, 0);
// 是系统软件或者是系统软件更新
if (isSystemApp(pInfo) || isSystemUpdateApp(pInfo)) {
return SYSTEM_REF_APP;
} else {
return USER_APP;
}
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return UNKNOW_APP;
}
/**
* 是否是系统软件或者是系统软件的更新软件
* @return
*/
public boolean isSystemApp(PackageInfo pInfo) {
return ((pInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0);
}
public boolean isSystemUpdateApp(PackageInfo pInfo) {
return ((pInfo.applicationInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0);
}
public boolean isUserApp(PackageInfo pInfo) {
return (!isSystemApp(pInfo) && !isSystemUpdateApp(pInfo));
}
}
转自: http://blog.csdn.net/pilou5400/article/details/6414832