4

I have an app 'A'. I am opening another app 'B''s video player and playing a video using an intent URI call like so

    String intentURI = "B://this/123";
    try {
        intent = Intent.parseUri(intentURI, Intent.URI_INTENT_SCHEME);
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        Logger.appendInfoLog("Something went wrong with B", TAG);
        e.printStackTrace();
        Logger.appendErrorLog(e.getMessage(), TAG);
        finish();
    }

startActivity(intent);

Now the necessary condition is for that app 'B' to be open in the background for this to work.If the app is closed(killed by Android or manually) or it has crashed,this throws an error.

Is there a way to open that app 'B' first or check its running status and then make the intent URI call. I will not get control back from that app and once I go to the other app I dont have any control on it until the user presses the back button to return to my app.

UPDATE:

I want to start app 'B' first and then call the intent programmatically. Is this possible

UPDATE

I Ran the Catlog app to check what message comes up in app B. It just shows file 123.file (The one i am trying to play in the app B's video player) not found, but when the app is running in the background it goes through fine. It also shows a warning

java.lang.NullPointerException: PrintLn needs a message

and then it says

Activity displayed, but mediaplayer.is not playingVideo

Also the other app is written in flash and packaged as a native app on adobe air

Vrashabh Irde
  • 14,129
  • 6
  • 51
  • 103
  • Why does app B need to be running? Can't you set this up so that launching this Intent will actually cause app B to start? – David Wasser Jun 04 '13 at 21:10
  • ^ How do I do this ? . In essence this is what I want. If the app is not running in the background I want to be able to go and start the app and the call the videoplayer. But if the app is running in the background, just call the videoplayer. – Vrashabh Irde Jun 05 '13 at 06:19
  • 1
    Android allows background processing - can't you just open app B, then send your intent? AFAIK your code should still execute in the background while B is open. – Richard Le Mesurier Jun 05 '13 at 06:59
  • I can. But sometimes app B shuts down in background either due to Android shutting down apps that arent active or due to low battery. Then I have to be able to restart that in the background and then call my intent – Vrashabh Irde Jun 05 '13 at 08:12
  • Could you post your Logcat when it fails to open 'B'? – Alejandro Colorado Jun 05 '13 at 08:26
  • It just says 123.file (The file in the app B I am trying to play by calling the intent) not found when the app isnt running in the background, otherwise it goes through fine. – Vrashabh Irde Jun 05 '13 at 08:42
  • Why do you keep the `startActivity(intent)` out of the `try/catch` block? – Alejandro Colorado Jun 10 '13 at 22:20

6 Answers6

7

I have an app 'A'. I am opening another app 'B''s video player and playing a video using an intent URI call like so

No, you are not. You can tell that by reading the code -- there is no startActivity() call in that code block.

Now the necessary condition is for that app 'B' to be open in the background for this to work.If the app is closed(killed by Android or manually) or it has crashed,this throws an error.

Then apparently app B has a bug. Please contact the author of app B for assistance.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • As he didn't include all the code, I think we were all assuming he did throw the intent with a startActivity, but you are right, he may have not. – Alejandro Colorado Jun 05 '13 at 13:19
  • Oops - look like i missed adding that piece of code in the question, updated now. But by Android design this should work right ? Even when the app B is closed or killed, my activity call should open their app given an intent URi of the above format – Vrashabh Irde Jun 06 '13 at 10:45
  • @Slartibartfast: Correct. Which is why if there is a problem of the type that you describe, it lies in app B. – CommonsWare Jun 06 '13 at 11:05
  • Awesome, let me test this and then get back to award the bounty – Vrashabh Irde Jun 06 '13 at 12:13
2

You can get a list of running apps easily.

ActivityManager manager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> processes = manager.getRunningAppProcesses();

And you can launch apps just as easily.

Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.package....");
startActivity(LaunchIntent);

You can't, however, launch an app into background, so this might not solve your issue.

unexpectedvalue
  • 6,079
  • 3
  • 38
  • 62
1

You don't need to open that app 'B' first. Just check if that app is running with:

// Get running processes
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> runningProcesses = manager.getRunningAppProcesses();

So now you have all the running processes in runningProcesses. Just iterate over the values to find out if your app 'B' is running. An example of this iteration can be found here:

ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
List l = am.getRunningAppProcesses();
Iterator i = l.iterator();
PackageManager pm = this.getPackageManager();
while(i.hasNext()) {
  ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
  try {
    CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
    Log.w("LABEL", c.toString());
  }catch(Exception e) {
    //Name Not FOund Exception
  }
}
Community
  • 1
  • 1
Alejandro Colorado
  • 6,034
  • 2
  • 28
  • 39
1

Define the intent listener inside of your manifest file.

See the docs for details.

Basically, what you would do is in your AndroidManifest.xml of App B (the app you want to start with the Intent), add a section like:

<receiver android:name=".MyIntentReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

Then inside of your MyIntentReceiver class, you would define the code to handle the intent.

public class MyIntentReceiver extends BroadcastReceiver {
    @Override
public void onReceive(final Context context, final Intent intent) {
        // handle intent here
    }
}
brianestey
  • 8,202
  • 5
  • 33
  • 48
  • I unfortunately have no control on app 'B'. its from another company. – Vrashabh Irde Jun 05 '13 at 06:20
  • Ooh... ok. Could you not just create an intent with Action=MAIN and Category=LAUNCHER? You'd have to know the component name of App B but if you know that you could launch B. – brianestey Jun 05 '13 at 06:26
  • I could. But after that, I then need to trigger the video player of the app using the intent I've put in the question. How do I get control back and then call this intent – Vrashabh Irde Jun 05 '13 at 06:34
  • Send the intent from a background task in App A. Perhaps an IntentService where the onHandleIntent just sends your intent and then finishes. – brianestey Jun 05 '13 at 07:59
  • Hmm yeah this seems like something feasible, let me explore it – Vrashabh Irde Jun 05 '13 at 08:13
1

I think your problem is that Intent.parseUri() returns an Intent with the action ACTION_VIEW (see http://developer.android.com/reference/android/content/Intent.html#parseUri%28java.lang.String,%20int%29). And from your code, I gather that android does not recognize that it actually has to start activity B for this. ACTION_VIEW is generic, so id does what seems to be most appropriate. If activity B does not fall into that category (probably not the standard video viewing app), it' will not get launched. I would suggest the following:

  1. Find out how to launch activity B (e.g. Intent i = new Intent.("com.packageB.ActivityB"); or similar. Developer should be able to tell you.
  2. Use i.setData() to set the data of your intent to your file.
  3. Use startActivity()

For step 2, there may be other ways Activity B needs the Uri passed. You can get this information from the developer as well. Using startActivity rids you of having to check whether App B is running. Something that Android does anyway.

thomi
  • 1,603
  • 1
  • 24
  • 31
0

I think it is possible to start another apk from your own, just check this answer. The problem is that if you dont know their uri, it will be hard to do what you propose. Now you can either ask the developers for the proper names or take a look at your own risk (i don't know about its legality).

Community
  • 1
  • 1
Neron T
  • 369
  • 2
  • 8