Reputation: 1219
I want to stop my service went it complete its tasks. but service wont be stopped I override the onDestroy() but it dose not work. When service stops it will start a new activity.
below is my code
DataService.java
package com.remote.synchronizer.haris;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.http.NameValuePair;
import android.app.Service;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class DataService extends Service {
boolean wifi,edge;
private Timer timer= new Timer();
SQLiteDatabase db;
String un,shop,city,date,order;
private SQLiteAdapter mySQLiteAdapter;
@Override
public void onCreate(){
super.onCreate();
mySQLiteAdapter = new SQLiteAdapter(this);
// this.stopSelf();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
final Handler mHandler = new Handler();
new Thread(new Runnable(){
@Override
public void run() {
Log.e("Service Started", "Successful");
while(true){
try{
Thread.sleep(10000);
mHandler.post(new Runnable() {
@Override
public void run() {
//Checking network connectivity
wifi=NetworkInfo.Wifi(DataService.this);
edge=NetworkInfo.EDGE(DataService.this);
if(wifi==true||edge==true)
{
int count=mySQLiteAdapter.getCCount();
int counter=0;
if(mySQLiteAdapter.getCCount()>0){
while(counter<count){
Log.e("Service Network", "Network is online");
int id=mySQLiteAdapter.getID();
List<NameValuePair> contacts=new ArrayList<NameValuePair>();
contacts=mySQLiteAdapter.getSingleRecord(id);
String url="http://10.0.2.2:3325/Product/Create?";
int response = 0;
try
{
response = CustomHttpClient.executeHttpPost(url, contacts);
if(response==200){
mySQLiteAdapter.delete_byID(id);
Log.e("Data Sent", "Response 200");
counter++;
}
else{
Log.e("Service Data", "Faield to upload data" );
}
}
catch (Exception e)
{
Log.e("Data Sending Error", e.toString());
e.printStackTrace();
}
}
}
//
Thread.currentThread().interrupt();
}
else
{
Log.e("Service Network", "Network is offline");
}
}
});
}
catch (InterruptedException e) {
Log.e("Data Sending Error", e.toString());
e.printStackTrace();
}
}
}
}).start();
return START_STICKY;
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onDestroy() {
Log.v("SERVICE","Service killed");
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_SHORT).show();
//timer.cancel();
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
callIntent.setClass(this, com.remote.synchronizer.haris.Login.class);
startActivity(callIntent);
super.onDestroy();
}
}
somewhere i have read that don`t stop the service Android will stop itself. If it is like that then kindly edit my code and tell how can i start a new activity when my work finish. Do i need to stop the timer? Then the onStartCommand will stop and it will call the onDestroy? if yes then how can i stop the timer because i have tried but no success.
Upvotes: 0
Views: 1762
Reputation: 666
onDestory() is used to release your resource,it will be called when the service is no longer used.
start activity like this:
final Handler mHandler = new Handler() {
public void HandleMessage(Message msg) {
if(msg.what == START_NEW_ACTIVITY) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
callIntent.setClass(this, com.remote.synchronizer.haris.Login.class);
startActivity(callIntent);
}
}
};
public int onStartCommand(Intent intent, int flags, int startId) {
new Thread() {
@Override
public void run() {
// do your job here
mHandler.sendEmptyMessage(START_NEW_ACTIVITY);
}
}.start();
}
Upvotes: 1
Reputation: 33996
To stop the service after your work has been finished simply call stopSelf()
or stopSelf(startId)
.
If you want to start the activity after the service has been finished, Before calling the stopSelf()
or stopSelf(startId)
, you should create the intent of the activity and set the flag mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
.
and start your activity by calling startActivity(intent)
Upvotes: 0