2014-12-30 82 views
1
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),但應用程序立即崩潰,我該怎麼辦?

+0

安置自己的logcat的。 –

回答

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匹配

上述方法只適用於您同時簽署apks w第i個same 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) 
     } 

    } 
} 

希望這有助於!

+0

只需將此代碼放置在類中的setContentView方法下面即可。 –

+0

在if語句中,如果安裝了pro鍵,並且使用與orignal應用程序相同的密鑰簽名,則可以執行任何你想要的操作... –

+0

sanskar你是偉大的人,你只是解決問題就像它沒什麼感謝 – user4400960

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; 
    } 
}