0
People were making an example for a service an application to be started on boot, it seems to start but it breaks off in a few seconds and I can’t understand why, I’m following an example of a book, and I found the same thing in many almost equal examples. Some hint because I can not understand why the service is interrupted.
public class NotificacaoTask extends TimerTask {
    private static int i = 0;
    @Override
    public void run() {
        Log.i("servico" ,"Incremento: "+ i++);
    }
 }
public class Servico extends Service {
 public static final String CATEGORIA = "servico";
 public Timer timer;
@Override
 public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i("servico","O servico foi iniciado!!!!");
    timer = new Timer();
    long delayInicial = 1 * 1000; //segundos
    long periodo = (long) 1 * 1000;
    timer.scheduleAtFixedRate(new NotificacaoTask() ,  delayInicial, periodo );
    return START_STICKY;
 }
public void onDestroy(){
    Log.i("servico" , "servico destruido");
    timer.cancel();
    super.onDestroy();
}
@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}
}
public class BootReciever extends BroadcastReceiver {
public static Intent service;
@Override
public void onReceive(Context context, Intent intent) {
    service = new Intent("INICIAR_SERVICO");
    context.startService( service );
    Log.i("servico","iniciou serviço boot");
}
}
<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.serviceboot.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name="Servico">
     <intent-filter>
     <action android:name="INICIAR_SERVICO" />
     <category android:name="android.intent.category.DEFAULT" />
     </intent-filter>
    </service>
    <receiver android:name="BootReciever">
        <intent-filter >
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
    </receiver>
</application>
logcat output
04-08 18:37:47.036: I/servico(704): iniciou serviço boot
04-08 18:37:47.057: I/servico(704): O servico foi iniciado!!!!
04-08 18:37:48.077: I/servico(704): Incremento: 0
04-08 18:37:49.186: I/servico(704): Incremento: 1
04-08 18:37:50.166: I/servico(704): Incremento: 2
04-08 18:37:51.077: I/servico(704): Incremento: 3
04-08 18:37:52.077: I/servico(704): Incremento: 4
04-08 18:37:53.077: I/servico(704): Incremento: 5
04-08 18:37:54.085: I/servico(704): Incremento: 6
						
Thanks using Alertmanager worked beauty, but got a little doubt, in my Service when it finishes normally it does not call onDestroy() ? or onDestroy() is only called by stopService()
– user4438
@user4438 If I am not mistaken, the call to onDestroy() is not guaranteed to be executed. It is better not to rely on it in all situations.
– Piovezan