How to Invoke or call one app from another app in Android?

痴心易碎 提交于 2019-12-02 11:38:26

I am going to assume that you really mean that you want to launch another app, not another Activity in your app.

Then there are two ways to do this. You can try using an implicit intent which according to the docs, an (Implicit) intent is "an abstract description of an operation to be performed" that "provides for performing late runtime binding between code in different applications." Sort of like trying to launch a method over the wire using an interface. You cannot be sure exactly what the class of the object that is launched only that it can handle the action and categories that you declare.

The second approach is an explicit intent, which is more like making a concrete call over the wire. If you know the package and class name this should work.

    Intent intent = new Intent(Intent.ACTION_MAIN);
    //intent.putExtra("plain_text", "Testing");
    intent.setClassName("packagename", "packagename.ClassName"); // Explicit Intent
    try {
        startActivity(intent);
    }
    catch (Exception e)
    {
        Log.d(TAG","onCreate",e);
    }
}

You can add extra info using flags depending on your needs and where your are trying to launch from.

JAL

Starting an external activity from your app is done using a slightly different method to that which you are using. You need to create an intent with a given action. For example, launching an intent to fetch an image from the gallery would look like this:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, IMAGE_PICK);

Note that you don't explicitly define the activity to be loaded, rather the kind of action you want to perform. Android will then pick (or have the user pick) an activity that has registered to handle this kind of intent to be run.

You might need to be a little more specific about what you're doing. If all you want to do is, say, launch another Activity from your main Activity, something like this would work:

Intent intent = new Intent(this, OtherActivity.class);
intent.putExtra("key", "data"); //put any data you want to pass into the new activity
startActivity(intent);

Then just make sure you put the new activity in your manifest like this:

<activity android:name=".OtherActivity"
      android:label="@string/other"/>

If your goal is something else then you should be ore specific with what you want to do.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!