Slim Shady
Slim Shady

Reputation: 1085

Intended Service from Activity stopped when app is closed

I have a service chatService which gets started when MainActivity is started. i.e when app runs.

I want to run this service always even when app is closed. But unfortunately when I close my app the service stops. I want to run the newMsg() in the background always when android boots or my app is started. But it closes when app is closed.

Can someone point me in the right direction?

MainActivity.java

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intService =  new Intent(MainActivity.this, ChatHeadService.class);
       startService(intService);
    }
}

chatService.java

public class chatService extends Service {      
        public void onCreate() {
            super.onCreate();
            new newMsg(this,null,null,null).execute(); 
       }
     ...
}

newMsg.java

protected void onPreExecute() {
     super.onPreExecute();  
}   

protected JSONArray doInBackground(Object... v) {
...
}
protected void onPostExecute(JSONArray json) {
    ...
    new Handler().postDelayed(new Runnable() {
        public void run() {
            new newMsg(main,...,...,...).execute();
        }
    }, 10000);      
}

Update 1

Transferred my new newMsg(this,10,null,null,null).execute(); to onStartCommand() and added return START_STICKY.

Guess what?

It doesn't make any difference!.

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    new newMsg(this,10,null,null,null).execute();
    return START_STICKY;
}

Upvotes: 0

Views: 112

Answers (2)

Umesh Chhabra
Umesh Chhabra

Reputation: 278

You can use RETURN START STICKY I'm using smartphone, so can not post the code.

Search RETURN START STICKY

public class Serv extends Service {

    String t,msg1;
    int id;

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        Timer timer = new Timer();

        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                // TODO Auto-generated method stub

                ConnectionDetector cd = new ConnectionDetector(getApplicationContext());
//ConnectionDetector  is custom class for detecting internet connection
//isCD() returns true if internet connection available


                if(cd.isCD())    
                   getData();

            }
        }, 0,360000);


        return START_STICKY;

    }

Upvotes: 1

Slim Shady
Slim Shady

Reputation: 1085

By replacing this on chatService.java file i was able to do the thing which i wanted. But it keeps the notification always on the top. Anybody who can tell me how to achieve this without notification on top it would be helpful and better.

chatService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    final int myID = 1234;

    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intent, 0);

    //This constructor is deprecated. Use Notification.Builder instead
    Notification notice = new Notification(R.drawable.ic_launcher, "Ticker text", System.currentTimeMillis());

    //This method is deprecated. Use Notification.Builder instead.
    notice.setLatestEventInfo(this, "Title text", "Content text", pendIntent);

    notice.flags |= Notification.FLAG_NO_CLEAR;
    startForeground(myID, notice);

    return super.onStartCommand(intent, flags, startId);
    //return Service.START_STICKY;
}

Upvotes: 0

Related Questions