Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keep application running in background

I want to keep my application running in background
I have an application that sends the user's location to our server
I have the following code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    LocationManager locationManager;
    String context = Context.LOCATION_SERVICE;
    locationManager = (LocationManager)getSystemService(context);

    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    String provider = locationManager.getBestProvider(criteria, true);

    updateWithNewLocation(null);

    locationManager.requestLocationUpdates(provider, (10*60*1000), 10,
                                           locationListener);
}
private final LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
      updateWithNewLocation(location);
    }

    public void onProviderDisabled(String provider){
      updateWithNewLocation(null);
    }

    public void onProviderEnabled(String provider){ }
    public void onStatusChanged(String provider, int status, 
                                Bundle extras){ }
  };
  public void updateWithNewLocation(Location location) {


        if (location != null) {
            Dbhelper helper = new Dbhelper(this);
            final SQLiteDatabase db = helper.getWritableDatabase();
            long time = System.currentTimeMillis();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
            final String curTime = df.format(time);
            final double lat = location.getLatitude();
            final double lng = location.getLongitude();
            final double alt = location.getAltitude();
            System.out.println(lat);
            System.out.println(lng);
            System.out.println(alt);
            db.execSQL("INSERT INTO location (longitude,latitude,altitude,tgl_buat) VALUES " +
            "('"+lng+"','"+lat+"','"+alt+"','"+curTime+"')");
            db.close();
            /*Timer timer = new Timer();
            timer.scheduleAtFixedRate(new TimerTask(){
                @Override
                public void run(){
                    db.execSQL("INSERT INTO location (longitude,latitude,altitude,tgl_buat) VALUES " +
                            "('121.2149012','-6.217837','0.0','2012-05-07 10:20:01')");
                    db.close();
                }
            }, 10*60*1000, 10*60*1000);*/

          } 
       }

I want my applicatioin to be running in the background. I want it to launch automatically when the phone is turned on

like image 759
akubabas Avatar asked May 08 '12 07:05

akubabas


People also ask

Is there a way to keep an app running in the background?

Start the Settings app and tap Battery & device care. Then tap Battery. On the Battery page, head to Background usage limits. Here, you'll find three different lists of apps that have some form of power management enforced on them.

How do I stop background apps from closing?

First, swipe down once from the top of the screen and tap the gear icon. Scroll down and find “Apps.” Next, tap the three-dot menu icon and select “Special Access.” If you don't see it, there will be a section on that screen titled “Special App Access.” Now select “Optimize Battery Usage.”


2 Answers

A very simple answer for your problem is to use Service. It will allow you to perform variety of tasks while being in background and is your best bet for sending your location to server silently.

Read this answer for help.

like image 145
waqaslam Avatar answered Nov 05 '22 18:11

waqaslam


You can keep your application running in the background using Service

I hope this link will help you

Please read the documentation for further details

like image 38
Hassy31 Avatar answered Nov 05 '22 18:11

Hassy31