Android - HashKey

非常麻煩的HashKey,基本上是設定FB後台必經過程

把下面程式碼 隨便在加在onCreate就可以了

 

PackageInfo info;
try{
   info = getPackageManager().getPackageInfo("package name",PackageManager.GET_SIGNATURES);
   for(Signature signature : info.signatures)
   {      MessageDigest md;
      md =MessageDigest.getInstance("SHA");
      md.update(signature.toByteArray());
      String KeyResult =new String(Base64.encode(md.digest(),0));//String something = new String(Base64.encodeBytes(md.digest()));
      Log.d("hashkey", KeyResult);
      Toast.makeText(this,"My FB Key is \n"+ KeyResult , Toast.LENGTH_LONG ).show();
   }
}catch(NameNotFoundException e1){Log.d("name not found", e1.toString());
}catch(NoSuchAlgorithmException e){Log.d("no such an algorithm", e.toString());
}catch(Exception e){Log.d("exception", e.toString());}