Android 编程下获得应用程序的签名
源码及安装文件下载方式一:GetAppSignature.zip
说明:应用程序的签名被封装在 packageInfo 中,所以我们要获得应用程序的签名就需要获得 PackageManager 来获得包含有签名信息的 packageInfo,再通过 packageInfo 获得 Signature 数组,循环遍历将签名进行拼接。
代码运行效果:
package cn.sunzn.getappsignature; import android.app.Activity; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.Signature; import android.os.Bundle; import android.text.TextUtils; import android.view.Menu; import android.view.View; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private EditText et_pkgname; private TextView tv_signature; private PackageManager manager; private PackageInfo packageInfo; private Signature[] signatures; private StringBuilder builder; private String signature; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_pkgname = (EditText) findViewById(R.id.et_pkgname); tv_signature = (TextView) findViewById(R.id.tv_signature); manager = getPackageManager(); builder = new StringBuilder(); } public void getSignature(View view) { String pkgname = et_pkgname.getText().toString(); boolean isEmpty = TextUtils.isEmpty(pkgname); if (isEmpty) { Toast.makeText(this, "应用程序的包名不能为空!", Toast.LENGTH_SHORT); } else { try { /** 通过包管理器获得指定包名包含签名的包信息 **/ packageInfo = manager.getPackageInfo(pkgname, PackageManager.GET_SIGNATURES); /******* 通过返回的包信息获得签名数组 *******/ signatures = packageInfo.signatures; /******* 循环遍历签名数组拼接应用签名 *******/ for (Signature signature : signatures) { builder.append(signature.toCharsString()); } /************** 得到应用签名 **************/ signature = builder.toString(); tv_signature.setText(signature); } catch (NameNotFoundException e) { e.printStackTrace(); } } } public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
专注移动互联网产品设计研发 分享最新的移动互联网产品和技术