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

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>

 

上一篇:如何为云原生应用带来稳定高效的部署能力?


下一篇:解密阿里云七武器之高性能消息服务ONS