应用程序在检测到应用时崩溃了

时间:2014-12-30 06:26:20

标签: android package-managers

package com.App.Detect;

 import android.app.Activity;
 import android.content.pm.PackageManager;
 import android.os.Bundle;
 import android.widget.*;

  public class MainActivity extends Activity
 {
    TextView t;
    @Override
    protected void onCreate(Bundle  savedInstanceState)
   {
        t =  (TextView)findViewById(R.id.TextView);
       super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
       //Put the package name here...
       boolean installed = isAppInstalled("com.my.detect.prokey");
       if (installed)
       {
        t.setText("App already installed on your android");
       }
       else
       {
           t.setText("Sorry,App is not installed on your android");
      }
   }

    private boolean isAppInstalled(String packageName)
{
        PackageManager pm = getPackageManager();
    boolean installed = false;
        try
    {
            pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
            installed = true;
    }
    catch (PackageManager.NameNotFoundException e)
    {
        installed = false;
    }
    return installed;
}
}

我正在尝试创建一个专业密钥来禁用广告并启用高级功能。

我搜索并发现此代码将“systemoutprintin”修改为t(textview)但应用程序立即崩溃我该怎么办?

2 个答案:

答案 0 :(得分:0)

通过checkSignatures方法很简单:

PackageManager manager = getPackageManager();
    if (manager.checkSignatures("<your_main_package_name>",
            "<your_pro_key_package_name>") == PackageManager.SIGNATURE_MATCH) {
        //act here(if the signatures of the two packages matches)
    }

这将检查两者,即两个应用的签名匹配,就像安装了专业密钥一样。

方法checkSignatures

  1. true如果安装了专业密钥且签名匹配
  2. false如果未安装专业版
  3. false如果安装了专业密钥且签名不匹配
  4. 只有在apkssame key

    同时签名时,上述方法才有效

    以下是完整的代码:

    package com.App.Detect;
    
    import android.app.Activity;
    import android.content.pm.PackageManager;
    import android.os.Bundle;
    import android.widget.*;
    
    public class MainActivity extends Activity {
        TextView t;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            t = (TextView) findViewById(R.id.TextView);
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            PackageManager manager = getPackageManager();
            if (manager.checkSignatures("<your_main_package_name>",
                "<your_pro_key_package_name>") == PackageManager.SIGNATURE_MATCH) {
                //act here(if the signatures of the two packages matches)
            } else {
                //act here(if the signatures of the two packages won't match or pro key isn't installed at all)
            }
    
        }
    }
    

    希望这有帮助!

答案 1 :(得分:0)

要检查所需的应用程序已经安装,请参考以下内容:

private boolean isPackageInstalled(String packagename, Context context) {
    PackageManager pm = context.getPackageManager();
    try {
        pm.getPackageInfo(packagename, PackageManager.GET_ACTIVITIES);
        return true;
    } catch (NameNotFoundException e) {
        return false;
    }
}