您的位置:首页 > 移动开发 > Android开发

判断Android设备是否已经安装某应用

2013-06-12 11:42 603 查看
MainActivity如下:

package cn.testinstall;
import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity;
import android.content.pm.PackageInfo;
/**
* Demo描述:
* 判断设备是否已经安装某应用
*/
public class MainActivity extends Activity {
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init(){
mTextView=(TextView) findViewById(R.id.textView);
String checkResult=CheckInstall("cn.testmatrix");
mTextView.setText(checkResult);
}
private String CheckInstall(String packageName){
String checkResult=null;
try {
PackageInfo packageInfo=this.getPackageManager().getPackageInfo(packageName, 0);
if (packageInfo == null) {
checkResult="未安装";
} else {
checkResult="已经安装";
}
} catch (Exception e) {
checkResult="未安装";
}
return checkResult;
}
}


main.xml如下:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:layout_centerInParent="true"
/>

</RelativeLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: