0

我有一项服务可以在一段时间内检查数据库,以检查 GPS 是否存在有效要求。如果有使用,它会启动 GPS 位置更新。我尝试使用不同的线程,但这会导致应用程序挂起。我发现应用在后台执行时表现更好。

我不断收到以下错误

无法在未调用 Looper.prepare() 的线程内创建处理程序。

我尝试使用 Looper,但由于某种原因,我似乎找不到正确的方法来实现它。我需要帮助无法运行的 PingListener,我确实通过添加删除了错误

Looper.prepare();

在 gpsPlotter 的 While 循环之前。

请忽略我留下的所有日志记录和一些垃圾变量,因为我喜欢在编码时跟踪所有内容。

public class PingListener implements LocationListener{
    public void onLocationChanged(Location pointer) {
        // TODO Auto-generated method stub
        String lat = String.valueOf(pointer.getLatitude());
        String longi = String.valueOf(pointer.getLongitude());
        new pingThread().execute(lat, longi);
        Log.d("Updating", "Sending Updates");

    }
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }
}


private class pingThread extends AsyncTask<String, String, String>{

    @Override
    protected String doInBackground(String... args) {
        // TODO Auto-generated method stub
        BeaconHandler db = new BeaconHandler(Ping.this);
        List<Beacon> beacons = db.getAllBeacons();

        int activecheck = 0;
        for(Beacon bn : beacons){
            activecheck = activecheck + bn.getStatus();
        }
        Log.d("Active Result", String.valueOf(activecheck));
        String ger = "";
        String postresponse = "";

          try{

            HttpClient retCode = new DefaultHttpClient();
            HttpPost retEmail = new HttpPost("http://www.site.com/tracker/return.php");

            List<NameValuePair> createValuePairs = new ArrayList<NameValuePair>();

            createValuePairs.add(new BasicNameValuePair("vert", args[0]));
            createValuePairs.add(new BasicNameValuePair("horz", args[1]));
            createValuePairs.add(new BasicNameValuePair("id", "2"));

            retEmail.setEntity(new UrlEncodedFormEntity(createValuePairs));

            // Execute HTTP Post Request
            ResponseHandler<String> createresponseHandler = new BasicResponseHandler();
            postresponse = retCode.execute(retEmail, createresponseHandler);

            ger = "Network Details\nLat:" + args[0] + "\nLong:" + args[1];
            Log.d("Sent: ", ger);
          }
          catch(Exception e){
              Log.e("Error", e.getMessage());
              Log.getStackTraceString(e);
          }

        return ger;

    }

    protected void onPostExecute(String value){


    }

}


private class gpsPlotter extends AsyncTask<String, String, String>{

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        int updateset = 0;
                    //Looper.prepare();
        while(true){
            try{
                BeaconHandler dbg = new BeaconHandler(Ping.this);
                List<Beacon> beacons = dbg.getAllBeacons();
                int activecheck = 0;
                for(Beacon bn : beacons){
                    activecheck = activecheck + bn.getStatus();
                }
                //db.close();
                if(activecheck > 0 && updateset == 0){
                    pinger.requestLocationUpdates(LocationManager.GPS_PROVIDER, 600, 0, reciever);
                    updateset = 1;
                    Log.d("GPS RESET", "Listener Set");
                }
                else if(activecheck == 0 && updateset == 1){
                    pinger.removeUpdates(reciever);
                    updateset = 0;
                    Log.d("GPS RESET", "Listener Removed");
                }
                else{
                    Log.d("GPS RESET", "No Modifications");
                }
                Thread.sleep(20000);
            }
            catch(Exception e){
                Log.e("GPS RESET ERROR", e.getMessage(), e);
            }
        }
    }       
}
4

2 回答 2

0

使用IntentService而不是AsyncTask.

于 2013-08-06T02:21:44.293 回答
0

您需要在 while 循环之后放置 Looper.loop()

private class gpsPlotter extends AsyncTask<String, String, String>{

@Override
protected String doInBackground(String... arg0) {
    // TODO Auto-generated method stub
    int updateset = 0;
                Looper.prepare();
    while(true){
        try{
            BeaconHandler dbg = new BeaconHandler(Ping.this);
            List<Beacon> beacons = dbg.getAllBeacons();
            int activecheck = 0;
            for(Beacon bn : beacons){
                activecheck = activecheck + bn.getStatus();
            }
            //db.close();
            if(activecheck > 0 && updateset == 0){
                pinger.requestLocationUpdates(LocationManager.GPS_PROVIDER, 600, 0, reciever);
                updateset = 1;
                Log.d("GPS RESET", "Listener Set");
            }
            else if(activecheck == 0 && updateset == 1){
                pinger.removeUpdates(reciever);
                updateset = 0;
                Log.d("GPS RESET", "Listener Removed");
            }
            else{
                Log.d("GPS RESET", "No Modifications");
            }
            Thread.sleep(20000);
        }
        catch(Exception e){
            Log.e("GPS RESET ERROR", e.getMessage(), e);
        }
    }
    Looper.loop();
}       

}

于 2013-08-06T02:28:38.367 回答