停止后台线程服务运行的Android代码

停止后台线程服务运行的Android代码,android,multithreading,service,Android,Multithreading,Service,嗨,这是在后台运行服务的代码。它工作得很好。但当操作完成时,它不会停止。这是我的密码。一旦数据发送到服务器,我就无法停止服务,我正在调用delete方法删除sqlite和mythread.stop()中的数据;停止线程,但它没有停止。谁来阻止线程有任何解决方案 public class ServiceTest extends Service { List<NameValuePair> nameValuePairs; String timerss=null,addi

嗨,这是在后台运行服务的代码。它工作得很好。但当操作完成时,它不会停止。这是我的密码。一旦数据发送到服务器,我就无法停止服务,我正在调用delete方法删除sqlite和mythread.stop()中的数据;停止线程,但它没有停止。谁来阻止线程有任何解决方案

public class ServiceTest extends Service {

    List<NameValuePair> nameValuePairs;

    String timerss=null,additional=null;

    String loc=null;

    int localUname = 0;

    String sno;

    String deviceid;

    List<Contact> contacts;

    private static String TAG = ServiceTest.class.getSimpleName();
    private MyThread mythread;
    public boolean isRunning = false;

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

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate");
        mythread  = new MyThread();
    }

    @Override
    public synchronized void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
        if(!isRunning){
            mythread.interrupt();
            mythread.stop();
        }
    }

    @Override
    public synchronized void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Log.d(TAG, "onStart");
        if(!isRunning){
            mythread.start();
            isRunning = true;
        }
    }

    public void readWebPage(){
        DatabaseHandler db = new DatabaseHandler(ServiceTest.this);
        contacts = db.getAllContacts();
        for (Contact cn : contacts) {
            String log = " USERNAME: "
                    + cn.getID() + " ,loc: " +cn.getName();

            localUname =  cn.getID();
            sno = String.valueOf(localUname);
            loc = cn.getName();
            timerss = cn.getPhoneNumber();
//          deviceid = cn.getMacId();
//          additional = cn.getAdditonal();

            System.out.println("benbenarji:" + localUname + loc+timerss+deviceid+additional);

            System.out.println("benbenarji1:" + localUname + loc);

        }
        try {

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://ip:80808/ActIotWifiLockApp/LockWebServices/storeLogForUD");
            //add your data
            nameValuePairs = new ArrayList<NameValuePair>(4);
            nameValuePairs.add(new BasicNameValuePair("slno", sno));
            nameValuePairs.add(new BasicNameValuePair("macId", deviceid));
            nameValuePairs.add(new BasicNameValuePair("logdesc", additional));
            System.out.println("logdes:" + additional);
            nameValuePairs.add(new BasicNameValuePair("dateTime", "timerss"));
            nameValuePairs.add(new BasicNameValuePair("username", "loc"));

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            //Execute HTTP Post Request
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            final String response = httpclient.execute(httppost, responseHandler);

            System.out.println("ResponseScan: " + response);

//            runOnUiThread(new Runnable() {
//                public void run() {
//
//                    dialog.dismiss();
//                }
//            });

            if (response.contains("true")) {

                db.deleteAllData();
                mythread.interrupt();
                mythread.stop();

            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }

        } catch (Exception e) {
            System.out.println("Exception : " + e.getMessage());
        }
    }

    class MyThread extends Thread{
        static final long DELAY = 10000;
        @Override
        public void run(){
            System.out.println("ben:"+isRunning);
            while(isRunning){
                Log.d(TAG,"Running");
                try {
                    readWebPage();
                    Thread.sleep(DELAY);
                } catch (InterruptedException e) {
                    isRunning = false;
                    e.printStackTrace();
                }
            }
        }

    }

}
公共类ServiceTest扩展服务{
列出nameValuePairs;
String timerss=null,additional=null;
字符串loc=null;
int localUname=0;
串sno;
字符串设备ID;
列出联系人名单;
私有静态字符串标记=ServiceTest.class.getSimpleName();
私人神话阅读;
公共布尔值isRunning=false;
@凌驾
公共IBinder onBind(意图arg0){
返回null;
}
@凌驾
public void onCreate(){
super.onCreate();
Log.d(标记为“onCreate”);
mythread=新的mythread();
}
@凌驾
公共同步的void ondestory(){
super.ondestory();
Log.d(标签“onDestroy”);
如果(!正在运行){
mythread.interrupt();
mythread.stop();
}
}
@凌驾
公共同步void onStart(Intent Intent,int startId){
super.onStart(intent,startId);
Log.d(标签“onStart”);
如果(!正在运行){
mythread.start();
isRunning=true;
}
}
公开阅读网页(){
DatabaseHandler db=新的DatabaseHandler(ServiceTest.this);
contacts=db.getAllContacts();
用于(联系人cn:联系人){
String log=“用户名:”
+cn.getID()+,loc:“+cn.getName();
localUname=cn.getID();
sno=String.valueOf(localUname);
loc=cn.getName();
timerss=cn.getPhoneNumber();
//deviceid=cn.getMacId();
//附加=cn.getAdditional();
System.out.println(“benbenarji:+localUname+loc+timerss+deviceid+additional”);
System.out.println(“benarji1:+localUname+loc”);
}
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://ip:80808/ActIotWifiLockApp/LockWebServices/storeLogForUD");
//添加您的数据
nameValuePairs=新的ArrayList(4);
添加(新的BasicNameValuePair(“slno”,sno));
添加(新的BasicNameValuePair(“macId”,deviceid));
添加(新的BasicNameValuePair(“logdesc”,附加));
System.out.println(“logdes:+附加);
添加(新的BasicNameValuePair(“dateTime”、“timerss”);
添加(新的BasicNameValuePair(“用户名”、“loc”);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
ResponseHandler ResponseHandler=新BasicResponseHandler();
最终字符串响应=httpclient.execute(httppost,responseHandler);
System.out.println(“ResponseScan:+response”);
//runOnUiThread(新的Runnable(){
//公开募捐{
//
//dialog.dismise();
//                }
//            });
if(response.contains(“true”)){
db.deleteAllData();
mythread.interrupt();
mythread.stop();
}否则{
Log.e(“ServiceHandler”,“无法从url获取任何数据”);
}
}捕获(例外e){
System.out.println(“异常:+e.getMessage());
}
}
类MyThread扩展线程{
静态最终长延时=10000;
@凌驾
公开募捐{
System.out.println(“本:”+isRunning);
同时(正在运行){
Log.d(标签“运行”);
试一试{
阅读网页();
睡眠(延迟);
}捕捉(中断异常e){
isRunning=false;
e、 printStackTrace();
}
}
}
}
}

您的
onDestry
实现包含以下行

if(!isRunning){
你是否需要说:

if(isRunning){
因为我假设如果
isRunning
true
,您希望停止线程

除此之外,文件还提醒我们以下几点:

您有责任通过调用stopSelf()或stopService()在服务完成时停止服务