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/
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 am using the code to call an intent in Android
Intent intent = new Intent();
String PACKAGE_NAME="com...."
intent.setPackage(PACKAGE_NAME);
intent.setAction(Intent.ACTION_VOICE_COMMAND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
getApplication().startActivity(intent);
Unfortunately, in some case, I do not know the PACKAGE_NAME. So, another method is using broadcast. How can I use it? Thanks all
You cant call Activity directly if you dont know package and name of that Activity.
Since you dont know package and Activity name you can only ask OS to show all possible variants for your Intent Action. In most cases user click "always open this app for this action" what means that you will directly open another app.
So in your case your code should looks like
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VOICE_COMMAND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
//dont forget to check if user has at least one application for your Intent Action
if (intent.resolveActivity(getPackageManager()) != null) {
context.startActivity(intent);
}
I want to update an apk through code. However I need to establish a mechanism that checks when that application has updated. I need to somehow get a message back from the intent
Context ctx = getApplicationCOntext();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File("/mnt/sdcard/Download/update.apk")), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(intent);
This is the default mechanism to install an application. How can I check that if the intent has finished?
Use startActivityForResult() if you want data to be return from the called activity. For more details, read: Getting a Result from an Activity How to manage startActivityForResult on Android?
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);
Consider am having 2 applications named first and second. The first applications has activity A the second applications has activity B. Initially Activity A of first application is launched and later second application will be launched from the activity A of the first application. Activity A of the first application has a count down timer. Once the timer hits How can i bring the activity A, being the application second is in visible ?
Thanks in advance.
Are you sure you need them to be different applications? Couldn't they just be activities in the same applications?
That said, you could just use an intent and start your activity that has an intent-filter for that intent?
hi try something like below code
final Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
final ComponentName cn = new ComponentName("com.urbanairship.airmail", "com.urbanairship.airmail.MainListActivity");
intent.setComponent(cn);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity( intent);