Android : Call activity of another application

Grant,

The issue here is clearly a misunderstanding of the Android Application Model. Commonsware is absolutely correct about how to solve this problem. However, without understanding Android fundamentals, I can see why you are having difficulty applying it. So, a quick explanation:

Every action in Android begins with an Intent. This is particularly true for Activities. Every Activity has an Intent. To make the interface easy for the developers, you may respond to an Intent from the OS, OR you may create an Intent from the Activities class to use. In general, it is best practice to do the first option.

Responding to an Intent

When picking an Intent to respond to, you may literally respond to any Intent. This is called an Action. If I created an Intent called “FOO”, the Bar Activity could pick it up and respond. We have conventions, however, and the primary of those is to prepend your package name to any Intent you make. For example “com.company.package.FOO”. Simply put, this is so that we avoid collisions with other apps.

Every Activity may respond to different events. This is defined in the AndroidManifest.xml.

<activity android:name="Activity3" ... >
    <intent-filter>
      <action android:name="com.company.package.FOO"/>
      <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

Above, we also set the category to DEFAULT, so that unless the user changes it, we’ll be the only app that responds to our custom Intent. The way that we then call the Intent is by using the SAME NAME that we created (i.e. “com.company.package.FOO”)

startActivity(new Intent("com.company.package.FOO"));

That’s how it works! You would simply change the above “com.company.package.FOO” to your package name (defined by your application) and something meaningful. An example is “com.testapp.ws.SWAT_FLIES”.

Why your code doesn’t work

Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.testapp.ws");

The above code looks for a specific KIND of Intent action. Remember when you made the AndroidManifest and the first Activity you put:

 <action android:name="android.intent.action.MAIN">
 <category android:name="android.intent.category.LAUNCHER">

Well… getLaunchIntentForPackage() only gets the Intent for that first Activity. That’s WHY we make a custom Intent… First, because we don’t really want it to be our 3rd Activity to be our start up… And second, becuase the OS will tell us only the startup Activity. We have to tell it with our OWN action (i.e. “com.testapp.ws.SWAT_FLIES”)

Hope this helps,

FuzzicalLogic

Leave a Comment