[:en]Android: always run in background[:]

[:en]Step 1: YourService.java

Highlight “app” on the top left corner of the screen, then right click and select “Service” then “Service” to create a service class:

  package com.cambridgeways.com.actionbardemo;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.Toast;

public class YourService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Toast.makeText(this,"24/7",Toast.LENGTH_LONG).show();
        // do your jobs here
        return super.onStartCommand(intent, flags, startId);
    }
}  

Step 2:


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Start a 24/7 service
        startService(new Intent(this, YourService.class));

    }

}
 

[:]

Print Friendly, PDF & Email
Scroll to Top