how to structure my app to run in background

I am new to Android, and I need some advices for start up.
I want to build an application, which will show up, when the user gets into some hot situation.

By hot situation I mean:

  • the GPS/cell coordinates are in known zone;
  • known Bluetooth device detected;
  • known Wi-Fi network detected;
  • weather info has change;

I see something running in background and when one of the clauses hit, it will trigger and open the app.

  • How to get started?
  • How do I make sure my app won't be shut down?

As I read somewhere that Android OS will terminate apps if memory out occurs or consumes too much, and my app would consume a lot, making repeated measures/checks to see if situation changed.


Asked by: Tara585 | Posted: 25-01-2022

Answer 1

You need to use a Service for the part of your application that runs in the background.

You might find the Application Fundamentals document in the Android Developer Documentation helpful. It says this about Services:

A service doesn't have a visual user interface, but rather runs in the background for an indefinite period of time. For example, a service might play background music as the user attends to other matters, or it might fetch data over the network or calculate something and provide the result to activities that need it.

In you case you might find the LocationManager Service helpful. It is a system Service which will you can use to notify your application based on GPS position.

However, I think you'll have to write your own Services to monitor Wi-fi, Bluetooth and weather.

You can use the AlarmManager Service to get your Service to perform particular tasks at certain intervals.

Answered by: Patrick505 | Posted: 26-02-2022

Answer 2

It depends on how & where you want to deploy your application. In my experience it boils down to

  1. you create an application for a specific use case where battery drain matters less than accurate results (showcase situations, prototyping, ...)
  2. you want to distribute the application to users.

In case 1) just create one service that aggressively polls the sensors / web services. Use the AlarmManager to send a REFRESH intent (AlarmService.setRepeating(...) ).

That REFRESH intent will restart the synchronization service everytime, even if it was killed by the system. onStart() will be called everytime the REFRESH intent is emitted. You can do heavyweight setup logic in onCreate() as this will be called everytime the service is created after it was destroyed. WARNING: This will possibly drain the battery very quickly.

In case 2) I would create several services and let the user configure different polling intervals for each service to limit battery drain. I can see for example that bluetooth should be polled more regulary than GPS as it is more likely that a bluetooth device suddenly appears than a user moving extremely fast.

Weather sounds extremely expensive (network lookup, possibly triggering a network connection!)

Please do not try to be too persistent with your app in case 2). It usually makes a lot of sense for a phone to kill memory / power draining services.

Answered by: Rafael466 | Posted: 26-02-2022

Similar questions

android - What's better background structure for GridView

I'm developing an application at Android 4 who has many Tabs(ActionBar.Tab) and each Tab has a GridView whicth shows approximately 30 images (ImageView). Each image throws other activities when clicked. When execution of activity ends the system shows the Tab previously selected. This is the scenery for each Tab. The images for each Tab are load from database and put in a singleton List. Then exists only one List for imag...

android - How to avoid the ugly "boxes" when child views in a View has another color than the background (stateful drawables for example)

I have a simple ListView and on that ListView I have placed a number of custom defined Views. The CustomView has ImageView and two TextViews. The CustomView also has a "stateful drawable" as background, so that the background image (a 9-patch) changes if you press the Row in the ListView. When pressing the Row, the background image changes to a Red-ish thing. The problem is that when the background ...

Android drawing - Background caching

Quick question - I'm having control that's extending LinearLayout and I'm overriding it's onPaint method like this @Override protected void onDraw(Canvas canvas) { super.dispatchDraw(canvas); _background.draw(canvas); _object1.draw(canvas); _object2.draw(canvas); _object3.draw(canvas); // etc... } Every 40ms I invoke postInvalidate() in background and onPaint gets called on...

android - how to start activity when the main activity is running in background?

I created an application which enables the user to set whether he wants to receive notification while the application runs in background mode. If the notifications are enabled an activity should be started (the dialog should appear on the screen). I tried to enabled it the following way: @Override public void onProductsResponse(List<Product> products) { this.products = products; mobool...

android - View on press onpress: Change background color on press? How do I show that the View is being pressed?

I have, for the time being, a custom view with a 9-patch image as a border. That custom view is placed three times in a LinearLayout, so it looks like this: +------------------------+ | CustomView | +------------------------+ | CustomView | +------------------------+ | CustomView | +------------------------+ I have attached a click event listener...

audio - Android - how to do background threading properly?

Was wondering if anyone could help me on background threading on Android. I have a piece of code that records from the mic of the device and then plays back what it records through the ear piece(on 1.5). I am trying to run it in a thread but have been unsuccessful in getting it to run as a background thread. Currently it runs and locks up the activity so that all thats happening is the thread is ru...

android - How to set background color of a View

I'm trying to set the background color of a View (in this case a Button). I use this code: // set the background to green v.setBackgroundColor(0x0000FF00 ); v.invalidate(); It causes the Button to disappear from the screen. What am I doing wrong, and what is the correct way to change the background color on any View? Thanks.

Android Widget Text Background

I have an app with a widget but I am having some difficulty with the layout of the widget. The basic idea if the widget should look like an icon and have a little text tag under it like any other icon on the desktop. I found one example which uses an android:background for the TextView and uses a drawable XML: <shape xmlns:android=""> <solid...

How do I change the background of an Android tab widget?

My class extends extends TabActivity TabHost mTabHost = getTabHost(); TabHost.TabSpec tab1 =mTabHost.newTabSpec("tab1"); TabHost.TabSpec tab2 =mTabHost.newTabSpec("tab2"); tab1 .setIndicator("title tab1"); tab2 .setIndicator("title tab2"); mTabHost.addTab(tab1);mTabHost.addTab(tab2); TabHost.setCurrentTab(0 or 1) Can anybody guide me how do I change the background image or color of sel...

android - Set title background color

In my android application I want the standard/basic title bar to change color. To change the text color you have setTitleColor(int color), is there a way to change the background color of the bar?

Run a service in the background forever..? Android

I am doing a Battery Consuming research on the Android phone. I want to run a Battery Check every 10 min till the battery totally dies. I have been having problems to make it work. At my first try, I use a timer in a service class, and schedule the battery check every 10 mins. But soon I found that the service got paused when the screen goes off. Then I try to use AlarmService, I use a alarm call to wake my...

Still can't find your answer? Check out these communities...

Android Google Support | Android Community | Android Community (Facebook) | Android