Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm developing an app that has to connect to a server once every hour. This happens without the user having to open the app.

  1. Can apps run in the background? Meaning if someone using my app is playing a game when my app has to connect to the server, can my app connect without interrupting the user playing their game. I know the I-Phone only allows one 3rd party application to run at a time, is IOS different?

  2. Assuming that apps can run as background processes, can they stay open for ever? I know that almost all of the newer phones don't actually give the user the ability to truly terminate a application and the system will actually terminate programs whenever it wants.

  3. If apps can't run in the background and stay open for ever, then can an app be automatically opened by the phone every hour?

I've been looking at alarm clock apps, as they seem to be a background process that open/ run at a set time, which is exactly what I want my app to do. Sadly I can't find any good source code. All the alarm clock examples require the app to be open or the alarm won't go off. Any help would be much appreciated. (please don't link me to the google source, I have already looked at that)

share|improve this question

3 Answers

up vote 1 down vote accepted

1-> Yes your can app can connect to the Server in Background without interrupting the user. You need to use a background Service to achieve this.

2-> You can have a background service that keeps running but its really a bad idea , to keep it running for ever .This will drain the battery and use the memory ,cpu and network unnecessarily. System has control over the apps it can kill the background processes when required.

3-> I dont think that there is a provision by which you can automatically open an app. Unless you are using some kind of Service that sends a broadcast or an event to app that will launch it. This is a bad design again. User will not like if his phone will behave weirdly :)

share|improve this answer

Android has something known as

Services

Services along with Alarm Manager should solve your problem.

share|improve this answer

Yes you can create a simple service class Like this Sample

MainFest File

<service
        android:name="com.example.service.LocationUpdateSerivice"
        class=".LocationUpdateSerivice" >
        <intent-filter>
            <action
                android:name=".LocationUpdateSerivice"
                android:value="com.pausefablogin.service.MY_SERVICE" />
        </intent-filter>
    </service>

Java Code

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;



public class LocationUpdateSerivice extends Service implements LocationListener {


   @Override
   public void onCreate() {
       super.onCreate();


       //I Have Created this class for location update use the concept

   }


   private void UpdateLocation()
   {}
   @Override
   public void onStart(Intent intent, int startId) {      
       super.onStart(intent, startId);  
       Log.i(tag, "Service started...");
   }
   @Override
   public void onDestroy() {
       super.onDestroy();
       Toast.makeText(this, "Service destroyed...", Toast.LENGTH_LONG).show();
   }

   @Override
   public IBinder onBind(Intent intent) {
       return null;
   }

@Override
public void onLocationChanged(Location arg0) {


}


@Override
public void onProviderDisabled(String arg0) {
    // TODO Auto-generated method stub

}


@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}


@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}








}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.