
[Android]アプリケーションのリスト,自己署名を取り出すサンプル#tryAppList00

アプリケーションのリストを取り出す。
取り出しアプリケーションの自己署名を取り出す。
自己署名を比較するとアプリが改ざんされたときに比較することでチェックしたりすることができるみたい。
リリースビルドやデバッグビルドごとに変わるので、2種類は用意して比較する必要がある。
確認するにはPackageManager#getPackageInfoで取得する。
タップしたプログラムの自己署名を取り出す。
検索した事
android 自己署名
android application signature 確認
android アンロックキー
android 解除キー
開発環境
Eclipse IDE バージョン: 4.2.1
package com.example.tryapplist00; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.app.Activity; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // スケジュールのリストビュー ListView lv = (ListView)this.findViewById(R.id.listView1); final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1); lv.setAdapter(adapter); lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { // ______________________________________________________________________________ // タップされたパッケージのシグネチャを表示するようにする @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ListView listView = (ListView) parent; String title = (String) listView.getItemAtPosition(position); PackageManager pm = getPackageManager(); // 自己署名を取り出す try { // パッケージの情報を取り出す PackageInfo packageInfo = pm.getPackageInfo(title, PackageManager.GET_SIGNATURES); // 複数あるように調べるけど、普通は1個らしい for (int i = 0; i < packageInfo.signatures.length; i++) { String sig = packageInfo.signatures[i].toCharsString(); Toast.makeText(MainActivity.this, "SIG[" + i + "] " + title + " md5[" + getMd5(sig) + "]", Toast.LENGTH_SHORT).show(); Log.i("SIG[" + i + "] " + title + " md5[" + getMd5(sig) + "]", sig); } } catch (NameNotFoundException e) { // TODO 自動生成された catch ブロック e.printStackTrace(); } } }); // ボタンにリスナーをセット Button btn1 = (Button)this.findViewById(R.id.button1); btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // パッケージマネージャを取り出す PackageManager pm = getPackageManager(); // インストール済みアプリを取得する List<ApplicationInfo> listAppInfos = pm.getInstalledApplications(PackageManager.GET_META_DATA); for (ApplicationInfo curr : listAppInfos) { // システムに登録されている物は無視する if ((curr.flags & ApplicationInfo.FLAG_SYSTEM) == ApplicationInfo.FLAG_SYSTEM) { continue; } adapter.add(curr.packageName); } } }); } // ______________________________________________________________________________ // ハッシュキー private String getMd5(String src) { // ハッシュキーに変換する byte[] hash = null; MessageDigest md; try { md = MessageDigest.getInstance("MD5"); md.update(src.getBytes()); hash = md.digest(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } // 16進文字列に変換する StringBuffer hexString = new StringBuffer(); for (int i = 0; i < hash.length; i++) { if ((0xff & hash[i]) < 0x10) { hexString.append("0" + Integer.toHexString((0xFF & hash[i]))); } else { hexString.append(Integer.toHexString(0xFF & hash[i])); } } return hexString.toString(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/button1" > </ListView> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:text="アプリリスト取得" /> </RelativeLayout>
[Android]AlarmManageを使って一定時間ごとに処理をするサンプル#tryServi...