Start service from main application icon - android

My application is basically a service, i.e. has no UI (there isn't any class subclassing Activity). I want to have it displayed in the list of applications and to run the service when it's started from there, with the advantage that the user can create a shortcut to display it in the home screen.
Is this possible?

Found the solution to this:
Create an Activity and in the onCreate(...) method call your service and then call finish().
To prevent the flashing of the Activity opening and closing, use this as the theme of your Activity (in the AndroidManifest.xml):
android:theme="#android:style/Theme.Translucent.NoTitleBar"

Just make one Activity that allows the user to configure your application. Also there surely must be some sort of UI as part of the app. Otherwise what is it actually doing and how does the user control it?

I want to have it displayed in the list of applications and to run the service when it's started from there, with the advantage that the user can create a shortcut to display it in the home screen.
That is not a "list of applications". It is a list of activities that have advertised via <intent-filter> that they are to appear in the launcher. The key word is "activities".
Creating a dummy Activity that starts the service and then finishes itself does the trick, but I'm a little worried about the flashing of the Activity loading and closing.
Then create an activity that adds value to the user.

Related

Android Launcher app: Is it possible to supply the user with different activities on home press?

I have made a launcher that launches an Activity on home press. However, I want to launch different activities depending on different circumstances. Is it possible to intercept the home-press and assign it a certain activity dynamically within my launcher before ever stating any activity?
What is possible here? I suppose I can setContentView(something else), but I'd rather have completely different activities launched.
If you don't need to launch a Activity specifically, you could just instantiate Fragments based on your logic. Otherwise, you can have a invisible Activity that does some logic quickly, then loads another Activity.
As a launcher, you can. Have a look into the onNewIntent callback
As of GINGERBREAD, if the Activity was already created and a new
Intent is being delivered to onNewIntent(Intent), any newly granted
URI permissions will be added to the existing ones it holds.

Android activity, service, notification and application launcher

I need your help because I want to write an Android app which works like this: when I tap the app launcher, I show an activity in which the user has to insert some data, like his name ecc. These data are passed to a background service through an intent and when the service starts I show a notification. When I click on the notification I show another activity in which the user can press a button to stop the service. Everything works fine but I would like that, if I tap again the app launcher but the service is active, the user didn't see the first activity (the one in which he has to insert the data), but the last one in which there is the button to press to stop the service. I don't have a clue of how to do. Can you help me please? Thanks.
Do you really need two Activities for these use cases? You can have just one Activity and two Fragments, one for starting the Service, and one for stopping. In your Activity's onCreate() method check if the Service is running and inflate the right Fragment dynamically.

Distinguish activity calling cases: from other activity/other package/by system

I'm making a simple e-book reader app, and an activity can be called by many cases.
I'd like to distinguish callee activity to know its origin action in case of
From my another activity: this can be easily solved by
StartActivityForResult from calling activity.
Called by back button click from other package app after share action ("whoops, I missed to click share button, and then back.").
Switched by user's multitasking choice.
Called by user click at the start screen: this might be known by MAIN entry point at the android manifest.
How to know above cases?
I have no idea why you would need to do this but...
1.From my another activity: this can be easily solved by StartActivityForResult from calling activity.
Yes, as long as the calling Activity is your own as you can't guarantee any 3rd-party code will use startActivityForResult(...). You can, however, use getCallingPackage() and getCallingActivity() in other cases.
2.Called by back button click from other package app after share action ("whoops, I missed to click share button, and then back.").
When the user presses the BACK button your Activity isn't being "called" - it's simply being resumed or re-started. The original calling app/Activity/method will still hold true - there is no way to ascertain that this has happened as the normal Activity life-cycle methods (onStart() and onResume()) are always called even when an Activity is first created.
3.Switched by user's multitasking choice.
If you mean using the "Recent" apps view, the same applies for my answer to 2. above.
4.Called by user click at the start screen: this might be known by MAIN entry point at the android manifest.
In this case onCreate() will be called although if your Activity is simply stopped for whatever reason, it may simply be restarted depending on the launch mode you use.
In short, I can't see you being able to gather much in the way of any accurate information as to how your Activity becomes active.
I am not too sure about the actual way for the above question as I am too a new guy in android.
But to the best of my knowledge... called by back button and switched by user's multitasking leads the activity to enter pause state.
So you can access it from "onPause" method in your activity.

Maintaining standard application Activity back stack state in Android (using singleTask launch mode)

I am having trouble finding out how to maintain the state of my Android app in development.
Just to clarify, I am not talking about maintaining activity state (i.e. keeping track of textbox values, checkboxes, etc on a specific activity).
Let's say for example my application has two activities A and B. When I start my app, it takes me to activity A, and pressing a button on it takes me to activity B. At this point, I press the home button on my phone to return to the main Android UI and exiting my app . However, if I choose to run my app again, it should take me to activity B, which is where I left off before pressing the home button, but instead it is taking me to activity A.
Does anyone know how I can rectify this?
(I am using a Samsung Vibrant in case if you need to know)
"However, if I choose to run my app again, it should take me to activity B, which is where I left off before pressing the home button, but instead it is taking me to activity A."
Yes, it should. If it isn't, you have done something in your app to tell the platform to modify its behavior. (Look at ApiDemos as an example, it uses the standard behavior, which is what it sounds like you are describing as what you expect.)
Things to look out for:
Don't use FLAG_ACTIVITY_NEW_TASK when launching activities.
Don't use the singleTask or singleInstance launch modes.
Don't see the clearTaskOnReset flag.
You're imagining there's something called an "Application" but that's an illusion. Your application is just a collection of Activities, Services, Receivers, etc.
If you look at the intent-filter tags in your manifest, you'll see that each icon in the home screen is associated with a filter like this:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
You can put that same chunk of XML on both of your Activities, and you'll get two icons in the home screen, one that always launches Activity A, and one that always launches Activity B.
What you may want to do instead is create a master Activity that launches one of the other Activities based on the shared state.
As for where to actually store the shared state, that depends on how complex your state is. This is a good place to start: http://developer.android.com/guide/topics/data/data-storage.html
As I understand the question you want to launch your application and have a different thing happen each time depending on where you left off the last time.
http://developer.android.com/reference/android/app/Application.ActivityLifecycleCallbacks.html The activity lifecycle is in the link. Your onActivityDestroyed method somehow needs to persist the present state and the oncreate needs to pick it back up. Persistence can be achieved via shared preferences, stored in a file, database or over the network http://developer.android.com/guide/topics/data/data-storage.html This unpredictable behavior could cause confusion for the end user if poorly implemented so use good judgement.

Best Practices for Activity Flow in Android Applications

Trying to understand best practice for the lifecycle of my android application, and how activities fit into it.
For example, I have a main activity, sort of the "home" of my application. But, on start-up there are several activities that I 'might' need to run, depending on several cases, one being that it is the first time the app's been run.
Is best practice to call these 'start-up'/house-keeping activities FROM my 'home' activity? Or should the application begin with a 'house-keeping' activities, do the work, then finish() and start the 'home' activity?
Thanks for advice about this,
-- J
For the best user experience (and cleaner code), you really shouldn't chain Activities.
A good best practice for the scenario you describe (needing a particular layout of options on first-launch) is to set a SharedPreference the first time that the "Home" Activity is created. In the same Activity.onCreate() call you should make a decision about what your UI will display based on that saved value (e.g., either set the appropriate View's visibility to View.GONE or choose a different layout.xml altogether).
As an added bonus: You can overload a hypothetical "has been opened" SharedPreference with the version number of the app (e.g., LastOpenedVersion) to be able to present the user with a change log the next time they open your "Home" activity after an upgrade.
I would set your LAUNCHER <intent-filter> on whatever the user will most likely want to go to from their home screen. Presumably, that would be your "home" activity.
In onCreate() of that activity, make the determination if there is some other activity that is needed (e.g., "first-run"), and call startActivity() on it. When the user presses BACK from there (or you finish() that new activity), control will return to your "home" activity.
One possibility is to start from a splash screen Activity (rather than a "home" one), which then determines what to launch next.
You should also consider if your start-up/house-keeping needs to be accomplished via an Activity. If it is not something that the user interacts with, then you can move that functionality into a Service that runs a separate thread.

Categories

Resources