我有一项服务,会在一段时间内检查数据库,以检查是否存在对GPS的有效要求。如果有用,则会启动GPS locationupdates。我尝试使用不同的线程,但它会导致应用程序挂起。我发现应用程序在后台运行时效果更好。
我一直收到以下错误
无法在未调用的线程内创建处理程序 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);
}
}
}
}
答案 0 :(得分:0)
使用IntentService
代替AsyncTask
。
答案 1 :(得分: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();
}
}