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

时间:2016-09-01 11:04:53

标签: android multithreading service

您好,这是在后台运行服务的代码。它工作正常。但是当操作完成时它不会停止。这是我的代码。一旦将数据发送到服务器并且我调用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();
                }
            }
        }

    }

}

1 个答案:

答案 0 :(得分:0)

onDestry的实施包含

if(!isRunning){

可能是你需要说:

if(isRunning){

因为如果isRunningtrue,我假设您要停止该帖子。

除此之外,文档还提醒我们以下内容:

  

通过调用stopSelf()或stopService(),您有责任在工作完成后停止服务。