I'm new to android and am trying to make an application that when the user presses a particular button in App A, he is sent to App B. The user can then come back to App A by pressing another button in App B. No content is transferred from one app to another.
I want to accomplish this by making custom intents for both the applications. How should I start with this? Also what exactly is Broadcastreceiver and do I need to use it for the above mentioned problem?
Thanks!
Switching between another Application can be by two ways that is
1.) If you know the MainActivity of the Application to Call, you use
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName(
"package_name","package_name.MainActivity"));
startActivity(intent);
2.) If you don't know the MainActivity to Call you just use PackageName, you use
Intent LaunchIntent = getPackageManager()
.getLaunchIntentForPackage("package_name");
startActivity(LaunchIntent);
I don't think you need a BroadCastReceiver here as it is what you use when you want to catch some event/action for eg- Low Battery. For further details check my answer here
See Code to launch external app explicitly (especially this answer). You'll have to create a custom intent for each of the applications, and then call that intent explicitly.
In App A Manifest:
<intent-filter>
<action android:name="com.mycompany.APP_A" />
</intent-filter>
In App B Manifest:
<intent-filter>
<action android:name="com.mycompany.APP_B" />
</intent-filter>
In App A Button Press:
Intent intent = new Intent();
intent.setAction("com.mycompany.APP_B");
startActivity(intent);
In App B Button Press:
Intent intent = new Intent();
intent.setAction("com.mycompany.APP_A");
startActivity(intent);
Related
I have a confusion with how android starts its launcher activities.
If I am declaring a launcher activity in the manifest file like this
<activity android:name=".Activities.Home">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
and I have an Application class which calls an activity based on a check like
if(ParseUser.getCurrentUser() == null){
Intent intent = new Intent(context,Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}else{
Intent intent = new Intent(context,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
which one has the precedence ? The manifest or the Application. What is the flow of events ? e.g. Application->Manifest (or) Manifest->Application (or) Application overrides Manifest ?
If I am receiving a notification in Android, my Application class is called. This makes the activities in the application class to be started, like shown above. Is there a way to detect who calls the Application class ? I mean whether the user starts it, or it starts from the notification which comes in ?
If there is a way to figure this out. How do I prevent the activity in the Application class to be called when I receive a notification ?
Thanks in advance.
Just pass the boolean extras from application and make the diffrence in the call from Application class and User Launch.
Intent intent = new Intent(context,Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("APP_CALL",True);
startActivity(intent);
Now in your Home activity Just check for APP_CALL if it is called from Application class then bool value will be true else false.
Also you can write in splash activity for checking login .
I hava a question about Android Receiver.
I'm possible to change System app.
B is the first app, when user turns on the power. But the problem is when user chooses FACTORY Mode(like setting language, google id...), B App has to be started finishing A App setting. That's why use android:enabled="false"and A App trigger B app. But not working.
I think "android.intent.action.BOOT_COMPLETED" send just one time after booting, so after changing enable receiver B app, it's not working. Is it right?
Please can you give me some advise?
A App
PackageManager pm = getPackageManager();
ComponentName compName = new ComponentName("com.test.myapp", "com.test.myapp.receiver");
pm.setComponentEnabledSetting(compName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 0);
B App AndoidManifest.xml
<receiver
android:name="com.test.myapp.receiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
B App
public void onReceive(Context context, Intent intent) {
if(Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())){
Intent startMainActivityIntent = new Intent(context, new.class);
startMainActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startMainActivityIntent);
}
Why don't you just start app B from app A directly? Yes, boot completed triggered only once. But you can start app B without any receivers, look here for example
As you know when you long press on the home screen, the phone shows up a list menu.
You can add shortcuts, widgets, Folders, etc. I would like my app to be in the shortcut list.
How can I do that?
Shortcuts had existed since API level 1, and can be used by 3rd party apps as well.
To add an activity to the shortcuts manu simply add this intent filter to your activity in your manifest:
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
To make the activity plant a new icon with an intent on the home screen, do this before finishing:
Intent intent = new Intent();
Intent launchApp = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchApp);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "My shortcut");
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, myIcon);
setResult(RESULT_OK, intent);
Change the launchApp intent to whatever you want launched when this is clicked.
See here: http://developer.android.com/reference/android/content/Intent.html#ACTION_CREATE_SHORTCUT
That functionality is already there by default, they just have to choose from the "Applications" list. You can't add directly to the main shortcuts list (it would get far too cluttered quickly), that's provisioned by the operating system and to my knowledge cannot be populated by a third party application.
Is it possible to create two entry points to an application in Android, I mean can I switch the main activity programmatically?
Every exported activity is a potential entry point into your app; a foreign app can start any of them with an intent. (An intent-filter comes with an implicit android:export.) You can however only have one entry point that the launcher will respect. To simulate a second launch-point, either
Provide a completely separate app with the purpose of starting one of your exported activities, or
Give your 'launch' activity the sole purpose of immediately starting one or another activity based on some logic (a saved preference, a phase-of-moon calculation, anything).
check this one below
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setComponent(new ComponentName(packageName,mainActivity));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(intent);
I think you are talking about launching activity decision based on some events, then you need to add a broadcast receiver, like by clicking on app icon on launcher if you want to start Activity1. then add intent filters to this activity Action_MAIN and ACTION_LAUNCHER, if you want to start Activity2 on phone boot up, then add filter to this activity, BOOT_COMPLETED.
If you are talking about to launch other apps from your apps then this can be the code:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setComponent(new ComponentName(packageName,mainActivity));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(intent);
I have an app that allows you to create Home "shortcuts" to a specific Activity. It turns out that some of my users will use the app, hit the home key to go do something else, then use one of the shortcuts to jump back to that Activity. Since the app is still in memory it just opens the new Activity on top of the others and the "Back" key will take them back through the whole history. What I'd like to have happen is if they use a shortcut then to effectively kill the history and have the back key just exit the app. Any suggestions?
First, set up the taskAffinity in the Manifest to make the Activity run as a different "task":
<activity
android:taskAffinity=""
android:name=".IncomingShortcutActivity">
<intent-filter>
<action android:name="com.example.App.Shortcut"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
then, when building the shortcut, set the FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TOP flags. Something like:
// build the shortcut's intents
final Intent shortcutIntent = new Intent();
shortcutIntent.setComponent(new ComponentName(this.getPackageName(), ".IncomingShortcutActivity"));
shortcutIntent.putExtra(EXTRA_STOPID, Integer.toString(this.stop_id));
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
final Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
// Sets the custom shortcut's title
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, custom_title);
// Set the custom shortcut icon
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.bus_stop_icon));
// add the shortcut
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(intent);
Try adding Intent.FLAG_NEW_TASK to the Intent.