0

I don't know why but every time a kill my app, also the notifications are removed and no service is created. It should start also on boot, but it doesn't seem to..

My manifest:

<?xml version="1.0" encoding="utf-8"?>

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<uses-permission android:name="android.permission.READ_CONTACTS" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="Test"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".auth.Splash"
        android:label="Ruby" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <!--
             android:enabled="true"
        android:exported="false"
    -->
    <receiver android:name=".service.AutoStart" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

    <service
        android:name="com.octo.android.robospice.JacksonSpringAndroidSpiceService"
        android:exported="false" />
</application>

AutoStart.java

package com.test.service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class AutoStart extends BroadcastReceiver {
    public void onReceive(Context arg0, Intent arg1) {
        Intent intent = new Intent(arg0, SimpleService.class);
        arg0.startService(intent);
        Log.i("Autostart", "started");
    }
}

SimpleService.java

    package com.test.service;

import com.test.R;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.TextView;
import android.widget.Toast;

public class SimpleService extends Service {
    public class NotifyMessage extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            TextView txt = new TextView(this);

            txt.setText("Activity after click on notification");
            setContentView(txt);
        }
    }

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

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(this, "Service created", Toast.LENGTH_LONG).show();
        final NotificationManager mgr = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification note = new Notification(
                R.drawable.abc_ab_bottom_transparent_dark_holo,
                "Android Example Status message!", System.currentTimeMillis());

        // This pending intent will open after notification click
        PendingIntent i = PendingIntent.getActivity(this, 0, new Intent(this,
                NotifyMessage.class), 0);

        note.setLatestEventInfo(this, "Android Example Notification Title",
                "This is the android example notification message", i);

        // After uncomment this line you will see number of notification arrived
        note.number = 2;
        mgr.notify(3, note);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Service destroyed", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onCreate();
        Toast.makeText(this, "Service started", Toast.LENGTH_LONG).show();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO do something useful
        return Service.START_NOT_STICKY;
    }
}

Splash.java

package com.test.auth;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.widget.TextView;

import com.test.R;
import com.test.mobile.MainActivity;
import com.test.service.SimpleService;

public class Splash extends Activity {

    // Session Manager Class
    SessionManager session;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set View to register.xml
        setContentView(R.layout.splash_activity);
        startService(new Intent(this, SimpleService.class));

    }
}
M4rk
  • 2,172
  • 5
  • 36
  • 70
  • The simple answer is not to "kill" your app if you want any part of it to still be working. Depending on android version, there are some ways to get a partial resume for example when swiping out of the recents list, but don't count on that working across versions or into the future. – Chris Stratton Oct 21 '14 at 19:12
  • Whatsapp for example, even if you kill it, it autorestart. How can I achieve this? – M4rk Oct 22 '14 at 10:28
  • Kill it **how**? On which android version? – Chris Stratton Oct 22 '14 at 13:10

1 Answers1

0

Services are depended from the App Instance. If you kill your app you also kill your Service. If you want to run services even the app is killed you need to start the Services via startForeground(). Also the BOOT Flag is not for "booting the app". In this context the Android OS Boot up is meant.

mapodev
  • 988
  • 8
  • 14