How to suspend my app? - android

I have an app that spends a lot of time generating bitmaps for use as wallpaper. When the user has selected a bitmap and applied it, I would like to go to the main launcher screen so it can be seen "for real".
I could just exit the app when the selection is made, but I would like to maintain state at least for a short time so that if the user wants to go right back in to change cropping or select a different bitmap, things are as they were before.
Andy ideas? Is there an Intent I could use to get back to the main launcher screen? Maybe simulate the "Home" button being pressed?
Thanks!

An intent to go to Home:
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_HOME);
startActivity(homeIntent);

Related

Open Apps Widgets screen Android

How to open the Apps | Widget screen?
I mean this screen
Got the issue, basically all you have to do is make an intent that chooses this.
Intent homeScreenIntent = new Intent(Intent.ACTION_MAIN);
homeScreenIntent.addCategory(Intent.CATEGORY_LAUNCHER);
homeScreenIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(homeScreenIntent);
Hope it helps.
Won't recommend but seems one of this will work but be careful:
moveTaskToBack(true) // Pushes the activity in stack to back.
and
finish() // Kills the app
Reference: Dev blog

start an activity without show it

I was trying to start activity from a service without showing it to the user, keep it work in background, I was searching a lot about that, and I found two ways to do that
by starting the activity then start the home main screen like this :
// this is for lunch the app
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.example.some");
// this is for going back
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// lets do it !
startActivity(LaunchIntent);
startActivity(startMain);
by putting a going back function in the activity itself, like this :
context.getActivity().moveTaskToBack(true);
BUT, in these two ways I have problems
in the first one, if the user was on another app, this operation will close his app and get him to home , more than that, some times the my activity not started but just be in the back without working i.e. if there was a song it isn't played
in second one, when the my activity started a black screen will appear for a second before it back to the home or previous user app
So , simply , this is what I want :
I want a behaviour equal to : the user open my app then he press back button, but without show that the app started unless he see the background apps
how to do that ?
For that, you can use an android Service.
See docs - http://developer.android.com/guide/components/services.html

Starting launcher and moving to default homescreen

I'd like to start a specific launcher (set by the user), which is working fine with the code below. However, the launcher isn't moving to the user's "default" screen (e.g. the leftmost screen), but rather to the one where the user last left off. Is there a way to tell the launcher to move to the user's default screen upon opening? I've tried Intent.FLAG_ACTIVITY_NEW_TASK and Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED.
Intent home = new Intent("android.intent.action.MAIN");
home.addCategory("android.intent.category.HOME");
home.setClassName(launcherPackage, launcherActivity);
startActivity(home);
I solved this by sending the above-mentioned Intent twice (once to actually show the homescreen, and the second time to move the homescreen back to the default desktop).

Android Launching or bringing to front another application via Intent

Im having trouble getting this to work, hereĀ“s a quick overview of the idea.
First, I cant change the logic behind this, it was a specific requirement from the customer, I realize that with any tool such as AnyCut it could be bypassed but that doesnt matter really.
My customer offers a suite of apps, the idea is that all applications bellonging to the suite would be launched from a "Dashboard app", so that I only show the Dashboard app in the main launcher and not all app icons.
Lets take two Apps to get the idea solved. The Dashboard App (A) and the Recieving App (B).
I want to establish an intent filter (I think) on app B so that whenever I go into app A, and click the app B icon the app will be either launched or started from where it let of (brought to front).
Is this even possible? If so, how can I do it? I managed to get it to launch by specifically launching one activity in the app using:
Intent i = new Intent();
i.setClassName("PACKAGE_NAME","SPECIFIC_CLASS");
startActivity(i);
But that isnt the behaviour that I want, as it always starts app B in the same spot.
Thanx in advance,
Stefano
Edit: Added some new information. I was taking a look at the DDMS.
If I launch the application from scratch through the main Android launcher the intent is exactly the same as when I leave the home button pressed and then only bring the app to front, what ever activity im in. So I was trying to reproduce, unsucsesfully until now, this intent.
INFO/ActivityManager(1292): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.package/.uiPackage.Activity}
This is how AnyCut does it
Intent { act=android.intent.action.VIEW flg=0x10000000 cmp=com.example.package/.uiPackage.Activity bnds=[125,242][235,360]}
Any idea how I could go about creating that exact same intent? I cant even find that flag in the Intent API.
Figured it out, this is how I did it.
Intent i = new Intent();
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setAction("android.intent.action.VIEW");
i.setComponent(ComponentName.unflattenFromString("com.example.package/com.example.package.activityName"));
startActivity(i);
I'm not quite sure I'm following the expected results you want to see, but the following would launch the app from the dashboard and remove the dashboard from the activity stack leaving the selected app running:
Intent i = new Intent();
i.setClassName("PACKAGE_NAME","SPECIFIC_CLASS");
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
I believe this should start the app as if you were starting any other app.
Please add more information on your logic if this is not what you are looking for.
I think that when you switch activities android's default action is to sort of pause or hold the activity in its state the user left it in last. I know there is a way to make it so that the state is not saved when switching activities but I cant remember it off the top of my head.

Android Launcher Shortcuts

I have made a simple punch in / punch out time clock application. I want to add the user the option of making a shortcut on the homescreen that will toggle the state of the app(time out / time in) but I don't want this shortcut to open up the app on the screen at all.
here is my setupShortcut()
private void setupShortcut() {
Intent shortcutIntent = new Intent(this, Toggle.class);
// shortcutIntent.setClassName(this, Toggle.class.getName());
shortcutIntent.putExtra(EXTRA_KEY, "ToggleShortcut");
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "ToggleShortcut");
Parcelable iconResource = Intent.ShortcutIconResource.fromContext(this, R.drawable.app_sample_code);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
setResult(RESULT_OK, intent);
}
Toggle.class is an activity that toggles the state for me. In the manifest I have these settings on it
<activity android:name=".Toggle" android:exported="true" android:theme="#android:style/Theme.Translucent.NoTitleBar">
As it is now I can create a shortcut on the home screen then press it. The first time I press it it starts the Toggle activity and completes it fine, but it also opens up the TimeClock activity on the screen. If I then hit the back button I go back to the home. I can now press this shortcut and it will start the Toggle activity and not change the screen. Before I added: shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); It would open up the TimeClock activity every time. Now it is just the first time. But I want it to never show anything on the screen when the Toggle shortcut is pressed. Does anyone have any idea of how to get rid of that?
The problem with launchers is that they start Activities, whereas what you want is to start a Service so you can modify the app state in the background. It sounds like what you really want to do is make a Widget, not a launcher shortcut. You can configure the widget to have a button that sets off a Service that will toggle things in the background.
You can "more simply" make a hidden activity that will do what you want!
This activity must use transparent themes so it never shows and call finish() at the end of the onCreate() without any particular conditions.

Categories

Resources