run the activity while it's not visible - android

I'm working now in a project to download images from the web and then show it after completing its download.
i made an activity class that has a button and text field and when the user press on the button, i started a service which will show a notification in status bar and after download i will show a message .
my main problem now is i want to go to other application or home page while the download is working.
i tried it but i after the download i got the message ,,, " Force quit"
should i use broadcast receiver or remote server to handle what i want.
Thanks,

The service should be the one downloading the content. If you go to the home activity our activity calls onPause, stoping all the work in progress.

Activities are just that, activities, they only work when visible and are built strictly for user interaction or a user activity, so this is impossible. Use a service instead. Here is a diagram showing lifespan of an activity:
Link to App activity explanation on Android developer site

You may need to call Looper.Loop() in your service to keep it running if you have any listeners in your service.

Related

Android pop up dialog from service

What is the way to create a popup screen that's opened from a background service while app is not running in foreground?
Refer to the Skype answer-reject dialog when there's an incoming call:
I have a background service that always runs even the app is not visible to user, but how can I pop up a screen like in the image from that service?
Update:
I am looking for the term used for this kind of screens, is it a regular activity started from the service, or some form of "notification" ?
This popup shows exactly that.
You can start an Activity with:
android:theme="#android:style/Theme.Dialog"
Do Read: If I can remember properly, I have read somehwere that it is against the Android guidline of using popup from a service. It recommends using a Notification service.

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

Launch android app after start and block other apps

I'm facing the following problem. I want to make an android device to run only my application. All other apps and phone feautes should not be available to a user.
The reason why I want to achieve this is simple: I want to destribute devices with preinstalled application to my client but I don't want to let them use all phone featues.
This could work this way: just after android boots my application is launched automatically and than somehow all other staff is blocked.
Do you have any suggestions how to achieve that? Is it possible? Do I need to root a device?
I hope you get my problem. Any advice you can give will be greatly appreciated.
This is a bit crude way. But see if it is of any help.
If you create your application as a launcher it will start on boot(using system broadcast BOOT_COMPLETED).
Then you need to override all the three android buttons Home, back and recent apps.
To override back button you just have to override the onBackPressed() method.
For home button you will start a service which will run in background. This service will check if your app is in foreground or not. if not then it will bring it in foreground. so when the user presses home the service will sense the foreground app isnt yours and launch it.
But for this switching back of your app android takes approx 3 to 5sec. In that period of time you can display a warning text which will block the user from stopping the service from settings.Also if you remove the activity animations then it will appear seamless.
For recent apps button the above trick will do. If the user tries to close the app using this button your background service will sense it and launch your app.
so its the background service that does all the trick. if you some how stop the service or uninstall the app you are through :p

What should I do when the android activity is interrupted?

I currently have a program where a user fills information out and sends it to a server. It then downloads a one to two MB file to device. I have it setup so the phone does not sleep when it is downloading the file, but it is interrupted when the user manually presses the sleep button or a text comes in to the phone. What would be the best solution to this? Should I create a service or is there someway to tell the activity to keep downloading in the onPause method?
won't it be better to download in the background using an android service and posting an android notification when it will be done?

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.

Categories

Resources