I use the following code snippet to launch an app on device:
Context mContext = getContext();
String packageName = getPackageName(); //the app to launch
Intent mIntent = getPackageManager().getLaunchIntentForPackage(packageName);
if(mIntent!=null){
mContext.startActivity(mIntent);
}
It works, the app get launched, however, I don't figure out a way to close the launched app by using the packageName.
How to close the launched app if I only know the package name?
You cannot close another app. Only the system can do that.
But if you are also the author of that other app, you could create a receiver in that app's activities that accepts an intent that tells it to finish() the activities.
You cannot close other application from your app.your problem has a workaround,after starting app based on package name you may send home intent which will give same experience to user.
Intent mIntent = getPackageManager().getLaunchIntentForPackage(packageName);
if(mIntent!=null){
mContext.startActivity(mIntent);
}
//sleep for 250ms or whatever time using handler
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
Related
Okay, I think I'm missing something here but can't seem to find a way around it :\
So this is my scenario, I have two apps, A and B. A Opens a B with the following intent:
PackageManager pm = getPackageManager();
Intent n = pm.getLaunchIntentForPackage(currAppInfo.getName());
n.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(n);
(currAppInfo is a custom object and getName returns the package name.)
Anyway, B is installing APKs. A receives the package installed broadcast and should be now moved back to front, how ever if I'm starting app A with an intent
Intent serviceIntent = new Intent();
serviceIntent.setClass(context, MainActivity.class);
serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(serviceIntent);
Instead of seeing A's mainActivity screen all I see is B's main activity screen.
Why is that? Is it the way I open the apps with the intents or am I missing something more basic here?
I call GooglePlay from my app through an intent and again after I kill my own app:
Intent intent = new Intent(Intent.ACTION_VIEW);
String sModule = "market://search?q=pub:mycompany";
intent.setData(Uri.parse(sModule));
startActivity(intent);
finish();
android.os.Process.killProcess(android.os.Process.myPid());
Task manager shows, that only GooglePlay is running. My app isn't there anymore.
So my focus is GooglePlay at the moment. When going to the Desktop through the Home-Button and calling my App again it directs me to GooglePlay again.
Why is that? How can I call GooglePlay from my app independently?
I expected that when starting my app again, which I had previously killed, it would start my app and not focus on google play.
keyword is "launchMode" and "task".
this type of problems are so annoying and much complicated in android.
but this time you can try this.
Intent intent = new Intent(Intent.ACTION_VIEW);
String sModule = "market://search?q=pub:mycompany";
intent.setData(Uri.parse(sModule));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
if u cant solve, try combine another flags with FLAG_ACTIVITY_NEW_TASK.
cheers!
I have two standalone applications i.e A and B. A needs some functionality from B. Application A calls an intent of Application B. Application B receives the request and process it and send back the result to A. Just like the Zxing Barcode Application.
How could i achieve the above? Any idea?
final Intent intent = new Intent(Intent.ACTION_MAIN, null);
final ComponentName cn = new ComponentName("com.your.package","package.class");
intent.setComponent(cn);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(intent);
or
Intent intent = getPackageManager().getLaunchIntentForPackage("com.your.package");
startActivityForResult(intent );
Replace com.your.package with Application B package id.
Actually, it reminds me of startActivityForResult method.
http://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent, int)
Rather good example:
http://www.vogella.com/articles/AndroidIntent/
I Want to design an App. Which will start on mobile restart.
I am able to start my activity by using "android.intent.action.BOOT_COMPLETED" intent
but "SAMSUNG NOTE" has facility to restart mobile not needed to switch off and then on
here i am facing problem my app is not starting on "restart"
Problem Solved for now by removing code which is commented.
Intent startMainActivity = new Intent(context,MyActivity.class);
startMainActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startMainActivity);
/* Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startMain);*/
I was using commented code to launch home screen after my app is launched
I have minimized my app using this..
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
The app is waiting for some event while it is in minimized state.How can i return to the app activity when some broadcast event occurs?
i can access the minimized activity from the the broadcast receiver class(using an object passed to it earlier) but it is running in the background of home screen.
and does android kill minimized apps eventually?
Intent homeIntent = new Intent(this, HomeActivity.class);
homeIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(homeIntent);
this will start the HomeActivity, if the activity still in memory, it will just bring it to front.
You can start the activity again using the Intent.
Example :
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("com.package.address","com.package.address.MainActivity"));
startActivity(intent);
If you want to maximize the application you need user interface in that case you can add your application in notification bar, once user click on notification bar you can open in your word can maximize the same. Adding your application in to notification bar you can get many tutorials :
Notification