2

I have two application and i want to start second application from first one when button pressed. However i dont want the second one to come front. It will do its work on background. How can i do this? Please give me a guide. I tried this but it opens the application and comes front and thats not what i want

       Intent intent = getPackageManager().getLaunchIntentForPackage(
                "com.example.abc");
        if (intent == null) {
            Toast.makeText(this, R.string.app_not_found, Toast.LENGTH_SHORT)
                    .show();
        } else {
            intent .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent .putExtra("str", "str");
            startActivity(intent );
        }

Thanks in advance

mystery
  • 153
  • 1
  • 1
  • 14
  • 1
    Reading your question and your comments to the answers that have been provided leads me to think that your architecture is flawed. It sounds like you need 2 different things: You need an `Activity` that is launched by the user, that has UI; and you need a `Service` that other applications can use to reuest that your application do something for them. These are completely different things. – David Wasser Dec 27 '13 at 14:06

2 Answers2

2

You are calling an activity so it will open up the activity in front. You should call BroadCastReceiver, this will open up in background. Click Here this is how i have done using BroadCastReceiver.

Community
  • 1
  • 1
Anas Reza
  • 646
  • 2
  • 9
  • 28
0

Activities are used when you want a UI. They should not be dealing with background tasks if your code architecture is good.

What you need to be doing instead is separate the "background" stuff of the other activity into an exported Service, and call that instead.

npace
  • 4,218
  • 1
  • 25
  • 35
  • my application should run with UI when directly opened but it must run in background when opened from another application – mystery Dec 27 '13 at 13:02
  • Yes, this is why when you open it directly, you start an Activity that starts the Service for the background stuff. And you don't launch your app from another application, you start its exported Service. – npace Dec 27 '13 at 13:11