How to make application running in the background all the time

5

I tried to use Service as they said but it is not working yet. I do not know if I understand correctly, will the onStartCommand() method run all the time? Because I've debugged and the application only goes into this method once, when onCreate() is called and I need what's there to run all the time. Here is the code:

Servico.java

public class Servico extends Service {
    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("oi", "onStartCommand");
        boolean ok = true;
        while(ok == true){
            try {
                Thread.sleep(30000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            gerarNotificacao();
       }
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}

RunService.java

public class ExecutaServico extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
            Intent pushIntent = new Intent(context, Servico.class);
            context.startService(pushIntent);
        }
    }
}

AndroidManifest.xml

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name=".ExecutaServico">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
        <service android:name=".Servico"/>

        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

MainActivity.java

 public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startService(new Intent(HomeActivity.this,Servico.class));
        final Button continuar = (Button) findViewById(R.id.btn_continuar);
        continuar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
        //varias coisas
            }
       });
   }
}

I have no idea what I did wrong ... Every 30 seconds the notification should appear

    
asked by anonymous 25.06.2016 / 05:22

1 answer

8

What you need is a Service , it runs even when your application is closed, and you can have it run even if the user restarts the device.

First of all, let's create your class of service

TestService.java

public class TestService extends Service
{
    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(TAG, "onStartCommand");
        // START_STICKY serve para executar seu serviço até que você pare ele, é reiniciado automaticamente sempre que termina
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}

In the second step, we'll create a BroadcastReceiver to notify the service to start as soon as the device starts.

BootCompletedIntentReceiver.java

public class BootCompletedIntentReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
            Intent pushIntent = new Intent(context, TestService.class);
            context.startService(pushIntent);
        }
     }
}

Now we will also declare in your MainActivity to start the service.

MainActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startService(new Intent(getBaseContext(), TestService.class));
}

Finally, we have to declare in Manifest Service , BroadcastReceiver and permission to start the service as soon as the device finishes booting.

Manifest.xml

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
 <application>
  <receiver android:name=".BootCompletedIntentReceiver">
   <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />
   </intent-filter>
  </receiver>
  <service android:name=".TestService"/>
 </application>

Doing so will give you a service that runs all the time, even if the device is restarted.

Some links that can help you too:

Autostart Service on Device Boot

How to automatically restart a service even if user force close it?

Android Services - Tutorial

Services | Android Developers

    
25.06.2016 / 13:44