android 判斷應用程序是否已安裝

1、判斷是否安裝
/*
* check the app is installed
*/
private boolean isAppInstalled(Context context,String packagename)
{
PackageInfo packageInfo;        
try {
            packageInfo = context.getPackageManager().getPackageInfo(packagename, 0);
         }catch (NameNotFoundException e) {
            packageInfo = null;
            e.printStackTrace();
         }
         if(packageInfo ==null){
            //System.out.println("沒有安裝");
            return false;
         }else{
            //System.out.println("已經安裝");
            return true;
        }
}


2、判斷後的邏輯: (轉自:http://ruixiazun.blog.163.com/blog/static/906879182013021115923732/)
//已安裝,打開程序,需傳入參數包名:"com.skype.android.verizon" 
if(isAvilible(this, "com.skype.android.verizon")){ 
                Intent i = new Intent(); 
                ComponentName cn = new ComponentName("com.skype.android.verizon", 
                        "com.skype.android.verizon.SkypeActivity"); 
                i.setComponent(cn); 
                startActivityForResult(i, RESULT_OK);    
            } 
//未安裝,跳轉至market下載該程序 
else { 
                Uri uri = Uri.parse("market://details?id=com.skype.android.verizon");//id爲包名 
                Intent it = new Intent(Intent.ACTION_VIEW, uri); 
                startActivity(it); 
            }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章