How to Invoke or call one app from another app in

2019-03-05 03:54发布

问题:

I want to invoke one application from another application.

My Java file code:

Intent intent = new Intent(Intent.ACTION_RUN);
intent.setComponent(new ComponentName("Package name", "class name"));
startActivity(intent);

But I'm getting problem in specifying exact package path and I don't know permission for that code in Manifest. Please, help me on this.

回答1:

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



回答2:

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.



回答3:

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.