Last active
August 29, 2015 14:15
-
-
Save shotaro-zz/9cafe50dfe457e2cb85c to your computer and use it in GitHub Desktop.
インストールされているアプリ一覧取得
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package dev.struggle.getappsample; | |
| import android.content.Context; | |
| import android.content.Intent; | |
| import android.content.pm.ApplicationInfo; | |
| import android.content.pm.PackageManager; | |
| import android.database.Cursor; | |
| import android.graphics.drawable.Drawable; | |
| import android.support.v7.app.ActionBarActivity; | |
| import android.os.Bundle; | |
| import android.util.Log; | |
| import android.view.LayoutInflater; | |
| import android.view.Menu; | |
| import android.view.MenuItem; | |
| import android.view.View; | |
| import android.view.ViewGroup; | |
| import android.widget.AdapterView; | |
| import android.widget.ArrayAdapter; | |
| import android.widget.ImageView; | |
| import android.widget.ListView; | |
| import android.widget.TextView; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| public class MainActivity extends ActionBarActivity { | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| // 端末にインストールa済のアプリケーション一覧情報を取得 | |
| final PackageManager pm = getPackageManager(); | |
| final int flags = PackageManager.GET_UNINSTALLED_PACKAGES | PackageManager.GET_DISABLED_COMPONENTS; | |
| final List<ApplicationInfo> installedAppList = pm.getInstalledApplications(flags); | |
| // リストに一覧データを格納する | |
| final List<AppData> dataList = new ArrayList<AppData>(); | |
| for (ApplicationInfo app : installedAppList) { | |
| AppData data = new AppData(); | |
| data.label = app.loadLabel(pm).toString(); | |
| data.icon = app.loadIcon(pm); | |
| data.pname = app.packageName; | |
| dataList.add(data); | |
| } | |
| // リストビューにアプリケーションの一覧を表示する | |
| final ListView listView = new ListView(this); | |
| listView.setAdapter(new AppListAdapter(this, dataList)); | |
| //クリック処理 | |
| listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { | |
| @Override | |
| public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | |
| ApplicationInfo item = installedAppList.get(position); | |
| PackageManager pManager = getPackageManager(); | |
| Intent intent = pManager.getLaunchIntentForPackage(item.packageName); | |
| startActivity(intent); | |
| } | |
| }); | |
| setContentView(listView); | |
| } | |
| // アプリケーションデータ格納クラス | |
| private static class AppData { | |
| String label; | |
| Drawable icon; | |
| String pname; | |
| } | |
| // アプリケーションのラベルとアイコンを表示するためのアダプタークラス | |
| private static class AppListAdapter extends ArrayAdapter<AppData> { | |
| private final LayoutInflater mInflater; | |
| public AppListAdapter(Context context, List<AppData> dataList) { | |
| super(context, R.layout.activity_main); | |
| mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
| addAll(dataList); | |
| } | |
| @Override | |
| public View getView(int position, View convertView, ViewGroup parent) { | |
| ViewHolder holder = new ViewHolder(); | |
| if (convertView == null) { | |
| convertView = mInflater | |
| .inflate(R.layout.activity_main, parent, false); | |
| holder.textLabel = (TextView) convertView.findViewById(R.id.label); | |
| holder.imageIcon = (ImageView) convertView.findViewById(R.id.icon); | |
| holder.packageName = (TextView) convertView.findViewById(R.id.pname); | |
| convertView.setTag(holder); | |
| } else { | |
| holder = (ViewHolder) convertView.getTag(); | |
| } | |
| // 表示データを取得 | |
| final AppData data = getItem(position); | |
| // ラベルとアイコンをリストビューに設定 | |
| holder.textLabel.setText(data.label); | |
| holder.imageIcon.setImageDrawable(data.icon); | |
| holder.packageName.setText(data.pname); | |
| return convertView; | |
| } | |
| } | |
| // ビューホルダー | |
| private static class ViewHolder { | |
| TextView textLabel; | |
| ImageView imageIcon; | |
| TextView packageName; | |
| } | |
| @Override | |
| public boolean onCreateOptionsMenu(Menu menu) { | |
| // Inflate the menu; this adds items to the action bar if it is present. | |
| getMenuInflater().inflate(R.menu.menu_main, menu); | |
| return true; | |
| } | |
| @Override | |
| public boolean onOptionsItemSelected(MenuItem item) { | |
| // Handle action bar item clicks here. The action bar will | |
| // automatically handle clicks on the Home/Up button, so long | |
| // as you specify a parent activity in AndroidManifest.xml. | |
| int id = item.getItemId(); | |
| //noinspection SimplifiableIfStatement | |
| if (id == R.id.action_settings) { | |
| return true; | |
| } | |
| return super.onOptionsItemSelected(item); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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="wrap_content" | |
| android:layout_margin="4dp" > | |
| <!-- アイコン --> | |
| <ImageView | |
| android:id="@+id/icon" | |
| android:layout_width="48dp" | |
| android:layout_height="48dp" | |
| android:layout_alignParentLeft="true" | |
| android:layout_alignParentTop="true" | |
| /> | |
| <!-- ラベル --> | |
| <TextView | |
| android:id="@+id/label" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:layout_alignParentRight="true" | |
| android:layout_alignParentTop="true" | |
| android:layout_toRightOf="@+id/icon" | |
| android:textSize="18sp" | |
| /> | |
| <!-- パッケージ名 --> | |
| <TextView | |
| android:id="@+id/pname" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:layout_alignLeft="@+id/label" | |
| android:layout_alignParentRight="true" | |
| android:layout_below="@+id/label" | |
| /> | |
| </RelativeLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment