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;
}