68
64

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Android開発 他のアプリを立ち上げるための方法

Last updated at Posted at 2018-01-21

##背景
Androidの開発ではよくある仕様です。自分のアプリから他のアプリを立ち上げることです。よく使われる方法はstartActivity(Intent)です.
IntentのsetClassNameメソードを使って、立ち上げたいアプリのpackageNameとclassNameを指定することです。startActivityを使ってIntentを引数として実行したら、自動的に他のアプリに飛ばして行きます。
まとめたコードは以下です

    String packageName = "jp.co.yahoo.android.yshopping";
    String className = "jp.co.yahoo.android.yshopping.YShopMainActivity";
    intent.setClassName(packageName,className);
    startActivity(intent);

問題がなければ,yahoo shoppingが立ち上がります。

##問題
それぞれの立ち上げたいアプリのpackageNameとclassNameをどうやって取得するのかが問題です。

####解決方法
私のやり方では,立ち上げたいアプリをとりあえず自分のスマホにインストールして,それから簡単なアプリに以下のソースコードを実行して,スマホの中にすでにインストールされた全てのアプリのpackageNameと立ち上げれるアプリのclassNameを出力する.出力した結果に立ち上げたいアプリを探せば,欲しい結果を取得できるはずです。
以下のソースコードを実行すれば,立ち上げれるアプリの情報と立ち上げれないアプリの情報を分けて処理できるようになる。


 PackageManager pm = getPackageManager();
 List<PackageInfo> pckInfoList = pm.getInstalledPackages(PackageManager.GET_ACTIVITIES | PackageManager.GET_SERVICES);
 for(PackageInfo pckInfo : pckInfoList){
     LaunchItem oLaunchItem = null;
     if(pm.getLaunchIntentForPackage(pckInfo.packageName) != null){
        String packageName = pckInfo.packageName;
        String className=pm.getLaunchIntentForPackage(pckInfo.packageName).getComponent().getClassName()+"";
                Log.i("起動可能なパッケージ名",packageName);
                Log.i("起動可能なクラス名",className);
     }else{
                Log.i("----------起動不可能なパッケージ名",pckInfo.packageName);
     }
     launchItems.add(oLaunchItem);
}

PackageManagerのgetLaunchIntentForPackageメソードは立ち上げれるかどうかを判断する


String className = pm.getLaunchIntentForPackage(pckInfo.packageName).getComponent().getClassName()+"";

####実践
スマホの全てのアプリをリストとして表示させ,立ち上げれるアプリをボタンをつけて,ボタンをクリックしたらアプリを立ち上げる.

######LaunchOtherAppActivity.java


public class LaunchOtherAppActivity extends AppCompatActivity {
    private ArrayAdapter<LaunchItem> launchItemsAdapter = null;
    private ArrayList<LaunchItem> launchItems = new ArrayList<LaunchItem>();
    private LaunchListAdapter mListAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launch_other_app);


        PackageManager pm = getPackageManager();
        List<PackageInfo> pckInfoList = pm.getInstalledPackages(
                PackageManager.GET_ACTIVITIES | PackageManager.GET_SERVICES);
        for(PackageInfo pckInfo : pckInfoList){
            LaunchItem oLaunchItem = null;
            if(pm.getLaunchIntentForPackage(pckInfo.packageName) != null){
                String packageName = pckInfo.packageName;
                String className = pm.getLaunchIntentForPackage(pckInfo.packageName).getComponent().getClassName()+"";
                Log.i("起動可能なパッケージ名",packageName);
                Log.i("起動可能なクラス名",className);
                oLaunchItem = new LaunchItem(true,packageName,className);
            }else{
                Log.i("----------起動不可能なパッケージ名",pckInfo.packageName);
                oLaunchItem = new LaunchItem(false,pckInfo.packageName,null);
            }
            launchItems.add(oLaunchItem);
        }

        mListAdapter = new LaunchListAdapter(this.getApplicationContext());
        mListAdapter.setmArrayList(launchItems);
        mListAdapter.setLaunchAppListener(new LaunchListAdapter.LaunchAppListener() {
            @Override
            public void onLaunch(Intent intent) {
                startActivity(intent);
            }
        });

        ListView launchListLayout = (ListView) findViewById(R.id.launchListLayout);
        launchListLayout.setAdapter(mListAdapter);

    }

}

######LaunchListAdapter.java


class LaunchListAdapter extends BaseAdapter {
    Context context;
    LayoutInflater layoutInflater = null;
    ArrayList<LaunchItem> mArrayList = null;

    LaunchAppListener mLaunchAppListener = null;
    public interface LaunchAppListener{
        void onLaunch(Intent intent);
    }

    public void setLaunchAppListener(LaunchAppListener _mLaunchAppListener) {
        this.mLaunchAppListener = _mLaunchAppListener;
    }

    public LaunchListAdapter(Context context) {
        this.context = context;
        this.layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return mArrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return mArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        convertView = layoutInflater.inflate(R.layout.activity_launch_other_app_sample_list,parent,false);
        TextView packageNameText = (TextView) convertView.findViewById(R.id.packageName);
        TextView classNameText = (TextView) convertView.findViewById(R.id.className);
        Button button = (Button) convertView.findViewById(R.id.launch);
        final String packageName = mArrayList.get(position).getPackageName();
        final String className = mArrayList.get(position).getClassName();

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mArrayList.get(position).isLaunchble()){
                    Intent intent = new Intent();
                    if(packageName != null && className!=null && mLaunchAppListener != null){
                        intent.setClassName(packageName,className);
                        mLaunchAppListener.onLaunch(intent);
                    }
                }
            }
        });

        if(mArrayList.get(position).isLaunchble()){
            packageNameText.setText(packageName);
            classNameText.setText(className);
        }else{
            packageNameText.setText(packageName);
            button.setVisibility(View.INVISIBLE);
        }

        return convertView;
    }


    public void setmArrayList(ArrayList<LaunchItem> mArrayList) {
        this.mArrayList = mArrayList;
    }

}

######LaunchItem.java


class LaunchItem{
    private boolean isLaunchble = false;
    private String packageName;
    private String className;

    public boolean isLaunchble() {
        return isLaunchble;
    }

    public String getPackageName() {
        return packageName;
    }

    public String getClassName() {
        return className;
    }

    public LaunchItem(boolean isLaunchble, String packageName, String className) {
        this.isLaunchble = isLaunchble;
        this.packageName = packageName;
        this.className = className;
    }

}

######activity_launch_other_app_sample_list.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:id="@+id/packageName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="packageName"/>
    <TextView
        android:id="@+id/className"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="classname"/>
    <Button
        android:id="@+id/launch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="launch"/>
</LinearLayout>

#####完成イメージ

#####結果
  LAUNCHボタンがついてある項目は立ち上げれるアプリ,LAUNCHボタンがついていない項目は立ち上げれない項目だ.LAUNCHボタンをクリックしたら,そのまま目標アプリに飛ばしに行くはず.
  元気でね,みんな~~^^~~

68
64
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
68
64

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?