Started Service stops if application is forcestop from download App - android

i made a sample android app,which starts a started service by calling startService(serviceintent).
it works fine,but if i forceQuit my application from settings>app>downloaded>force_Quit.my service stops and even destroyed is not called.
i studied for 3-4 days and know about start_sticky in StartOnCommand method.i am able to achieve all aspects of service.
I want to know whatever i am achieving that service stops and doesnot restart automatically even if started as Start_Sticky is normal behaviour according to android.Can i make it restared if user force quit my application.
my manifest is correct i uses process tag also.

if i forceQuit my application from settings>app>downloaded>force_Quit.my service stops and even destroyed is not called.
Correct.
Can i make it restared if user force quit my application.
No. Nothing of your app will run again until something uses an explicit Intent to start one of your components. Usually, that means that the user taps on your icon in the home screen launcher, though there are other explicit-Intent scenarios (e.g., GCM message).

Related

Sticky Service and Intent Service gets killed when the app is destroyed

In My app I am trying to download images even though user has force stopped it and exit it using recent app manager by swiping . Why I want this behavior, it has only one reason and that is :
Because I want User to stop the download from my button given in activity
So For this I have made async task at first place , it was working nicely even the activity is destroyed, But When I destroy/kill my app using recent app manger the app gets killed and so the service
Then I moved my async task to the Service which is Sticky , I thought it should not get destroy when the user closes the app from recent app lists, but The service stops. though some times it start again , but due to some reason I just want it to not to stop and download like a google play store does. What ever you do , you can not stop downloading of app , until and unless you use stop button in the google play store app.
Then I read about IntentService, about intent service I read that ,
You can run intent service and forget about it , as it stops itself
after completion of task.
So I though this is something , which could not be killed even user stops it or close app from recent app list by swiping , as intent service would perform its task and would stop itself
But All in vein. I have read many things about the service and how to cure service from being not killed. I came to know about starting service as forground.
But what other ways do other Top class apps adopt to keep the background working all the time , like security apps , gps based apps, and antivirus apps whose services run all the time on background. You can examine same behaviour inn google play store app , you can start downloading of any app and exit the play store app , even from recent app tray , but it would continue to download until and unless you go to notification bar click notification , get navigated to place where you finally see stop downloading button to stop downloading the app.
So How can I achieve this ? what is wrong with service or what else can I use to get the same behavior as google play store has.
Please guide me . Your comments and discussion would be appreciated.
It sounds like you want to run your service in a separate process from your main application so it does not get killed if the app is removed from the "Recent Tasks" list.
This is made possible by using the "Process" attribute explained in the documentation here:
http://developer.android.com/guide/topics/manifest/service-element.html#proc
What you need is a ForegroundService. The android developer guide about services states that:
A foreground service is a service that's considered to be something the user is actively aware of and thus not a candidate for the system to kill when low on memory. A foreground service must provide a notification for the status bar, which is placed under the "Ongoing" heading, which means that the notification cannot be dismissed unless the service is either stopped or removed from the foreground.
You can use return return START_STICKY in onStartCommand in service

Turn on/off flashlight while app is minimized

In my application I have an activity and a service. The service continously manages some data. In certain cases the service sends a broadcast request to the activity to turn on flashlight.
This works very well. However it does not work when app is minimized (manually or by incomming call). As soon as I bring the app back to the front it starts to work again.
As I observed the log I saw that the service is still running when app is minimized. However since the activity is not present anymore the camera object cannot be accessed to activate the flashlight. But there is no error in the log.
Why is the flashlight not activated? How can the flashlight be activated when the app is minimized?
Only the foreground activity can hold the camera. From the Google documentation:
"If your application does not properly release the camera, all subsequent attempts to access the camera, including those by your own application, will fail and may cause your or other applications to be shut down."
You can try claiming it on the service instead, but I suspect that won't work well. It just really wasn't designed to work this way.

Start my android application *in the background* on boot

I am successfully able to start my Android app automatically on boot using BroadcastReceiver with an intent-filter BOOT_COMPLETED. In my onReceive method, I start the launcher activity for my application.
However, I don't want this application to be in the foreground on boot, but I do want it to be on the activity stack. Is there a way to still have the home screen show up on boot, but also have my application starts up. (I don't think I want to use a Service, because my application has UI.)
Decision on weather you should or should not use Service in appliction is independent on weather it has UI or not. All "third party" apps have UI, there is little use of application without at least 1 activity.
So in your case, just use Service.

simple app which sends one sms without gui required

I want to write an app to send an sms from a shortcut on a home screen. That is all.
I just can't understand within the framework how I can write such an app. Here's what I've tried so far and what my ideas are:
I wrote an activity that sends an sms using SmsManager within the onCreate() however, this just keeps on sending messages even though the code is not in a loop. I realise I must be not be using an activity the way it's designed to be used. The android application fundamentals article says an activity is for displaying a screen or gui, but I don't need a gui. I just don't know what component I need to to use.
A service? no, because I don't need something running forever in the background.
An activity? I guess no because I don't need a gui.
I had an idea to create a broadcast receiver which would respond to a broadcast, so my sens smsm code would be in there ready to send when it receives the signal. But how do i send the signal from an app shortcut on the home screen? What would be the entry point of the app.
I'm just really confused, I've read the tutorials and the app fundamentals and searched forums and not found the answer. There's is just a big gap in my knowledge of the android framework that needs filling I guess, once it clicks I'll be fine but I'm just stuck right now.
Thanks people.
Service does not have to run forever. You can control how long it works in the background, you can even create Service that will shoot once and disappear. Suggestion:
from your shortcut (app icon) start Activity. That will be activity with translucent background. To achieve that skip line setContentView() and define theme
#android:style/Theme.Translucent
in your AndroidManifest.xml. This way you will avoid black screen flash at Activity startup.
from that Activity start Service and call finish() on that Activity
perform SMS sending (you already know how) from Service. Maybe, you do not even need Service, you can send SMS from the translucent Activity.
call stopSelf() from your Service immediatelly or after some short timeout (wait for SMS sending result).
All described can be done smoothly through Widget framework. In that case you can even have custom button user may press on. So, that would be another approach.

StartActivity intent fails when certain apps are running

I am running into a critical conflict of sorts. My app is a remote service which essentially starts an activity when the screen goes to sleep. How it does this is very simple via screen off broadcast receiver and then an explicit intent to start the activity as a new task. The activity is basically in charge of responding to key events and displaying some simple text.
Thanks to a few window flags added in 2.0, activities can do this. They can be created in a way that either puts them on top of the lockscreen, or completely dismiss the lockscreen. This way they basically have focus without the lockscreen having to be dismissed by user. The alarm clock in 2.0 uses the flags to wake up the device and show the alarm dialog. I use them to place my activity when the screen sleeps so the user sees a custom wakeup lockscreen. The reason we create it at screen off is to get rid of lag the user experiences at wakeup involving first seeing the lockscreen, then seeing the activity appear. Also doing it immediately at sleep allows it to have focus so it can handle key events effectively.
The process works perfectly except in certain apps. So far, it seems the bug is consistent while browser (and even dolphin browser) as well as the facebook app are running. The bug never happens in GTalk or Launcher. It is rare but can still be duplicated in the messaging app every so often. I can't figure out why my activity doesn't get created at sleep while these apps are active. My remote service still gets the screen off broadcast and does the startActivity for the explicit intent, and that's all I get in the log. My onCreate is not being called. Instead it gets called when we wake the screen up again.
I have tried, as a control, to hold the partial wakelock starting when my remote service gets created, and the issue persists. So I don't believe it is a problem that the CPU has gone to sleep. Since only these particular apps cause the issue to duplicate, I can't imagine why the activity start fails. What could those apps be doing to interfere with another app's ability to get created? I use singleInstance as the launch mode so that I can ensure that the activity will never be able to be recalled by user process. I want it to go away when user unlocks and it is working fine like this, as long as it is able to be created. The singleInstance ensures I can have the same lockscreen handle an intent to do something specific based on user actions that the remote service monitors.
my source code can be viewed on my project page. http://code.google.com/p/mylockforandroid/source/browse/#svn/trunk/myLock/src/i4nc4mp/myLock
the issue happens to both my CustomLockService and NoLockService variations. These two services will start Lockscreen or ShowWhenLockedActivity and the bug is witnessed. The build illustrating the bug's end result-- user has to try to unlock 3 times due to the bug because on wakeup when the oncreate finally succeeds, user is seeing the activity when normally it would have auto-dismissed thanks to key event logic that also isn't seeming to happen due to the delayed onCreate, so they have to send it to sleep again. Now that the activity is properly done being started, and screen is asleep, the expected functionality happens at next wakeup-- can be downloaded also from the downloads tab.
This seems like an extremely irrational thing to be caused only by specific apps. I am quite baffled and out of ideas for a solution unless I've made some critical mistake in my activity definitions.
The answer is actually a bug in android that has been in review for a while. It has to do with the home key. For some reason start activity calls as new tasks are getting stopped after the home key has recently been launched. I never noticed the connection during the testing of this. The bug was not consistent and the factor of consistency was whether home button had been used during the wake in question
Here is the bug report: http://code.google.com/p/android/issues/detail?id=4536

Categories

Resources