2011-02-11 77 views
1

我已經使用這個方案,定期服用全球定位系統,但我在那有異常,請給我解決廣東話的GPS detetction創建線程問題內部處理

 package com.excerciseTest; 



    import java.util.ArrayList; 
    import java.util.Calendar; 
    import java.util.List; 
    import java.util.Locale; 
    import java.util.Timer; 
    import java.util.TimerTask; 



    import android.app.Activity; 
    import android.app.AlarmManager; 
    import android.app.NotificationManager; 
    import android.app.PendingIntent; 
    import android.app.Service; 
    import android.content.Context; 
    import android.content.Intent; 
    import android.location.Address; 
    import android.location.Geocoder; 
    import android.location.Location; 
    import android.location.LocationListener; 
    import android.location.LocationManager; 
    import android.os.Bundle; 
    import android.os.CountDownTimer; 
    import android.os.IBinder; 
    import android.os.Looper; 
    import android.util.Log; 
    import android.widget.Toast; 

    public class GpsService extends Service{ 
      private Long counter = 0L; 
     private Timer timer = new Timer(); 
     private final Calendar time = Calendar.getInstance(); 
     private LocationManager locationManager; 
     private Location currentLocation; 

     @Override 
     public IBinder onBind(Intent intent) { 
    // TODO Auto-generated method stub 
    return null; 


} 
@Override 
public void onCreate() 
{ 
    super.onCreate(); 
    /*Intent intent = new Intent(this, RepeatingAlarmReceiver.class); 
    PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, intent, 0); 



    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (5 * 1000), 10 * 1000,pendingIntent); 
    Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show(); 

    */ 

    try 
    { 

     _startService(); 
     //Set Starting point of latitude and longitude 
     /* 
     counter=0L; 
     new CountDownTimer(3000, 1000) { 

      public void onTick(long millisUntilFinished) { 

       locationManager=(LocationManager)getSystemService(LOCATION_SERVICE); 
       locationManager.requestLocationUpdates("gps", (long)30000, (float) 10.0, new LocationListener() 
       { 
        public void onLocationChanged(Location arg0) 
        { 


          counter++; 
          System.out.println(counter); 
          handleLocationChanged(arg0); 
          /* 

          System.out.println("latitude"+lati.get(0).toString()); 
          System.out.println("langitude"+lati.get(1).toString()); 


          /* 
          if(counter==1) 
          { 
           checklatAt0=getLatitude(); 
           checklonat0=getLongitude(); 
           System.out.println("Latitude At 0"+checklatAt0); 
           System.out.println("Longitude At 0"+checklonat0); 



          } 
          if(counter==300) 
          { 

           checklatAt300=getLatitude(); 
           checklonat300=getLongitude(); 
           System.out.println("Latitude At 300"+checklatAt300); 
           System.out.println("Longitude At 300"+checklonat300); 
           counter = 0L; 


          } 
          System.out.println(counter); 


          counter++; 


      } 

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

      } 

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

      } 

      public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
       // TODO Auto-generated method stub 
      } 
     }); 




       //mTextField.setText("seconds remaining: " + millisUntilFinished/1000); 
      } 

      public void onFinish() { 
       //mTextField.setText("done!"); 
      } 
     }.start(); 
     */ 



    /* 
     timer.scheduleAtFixedRate(
       new TimerTask(){ 

        public void run() 
        { 
      /// Subscribe to the location manager's updates on the current location 
         locationManager.requestLocationUpdates("gps", (long)30000, (float) 10.0, new LocationListener() 
         { 
          public void onLocationChanged(final Location arg0) 
          { 



            ArrayList lati=handleLocationChanged(arg0); 


            System.out.println(lati.get(0).toString()); 
            System.out.println(lati.get(1).toString()); 



            if(counter==1) 
            { 
             checklatAt0=getLatitude(); 
             checklonat0=getLongitude(); 
             System.out.println("Latitude At 0"+checklatAt0); 
             System.out.println("Longitude At 0"+checklonat0); 



            } 
            if(counter==300) 
            { 

             checklatAt300=getLatitude(); 
             checklonat300=getLongitude(); 
             System.out.println("Latitude At 300"+checklatAt300); 
             System.out.println("Longitude At 300"+checklonat300); 
             counter = 0L; 


            } 
            System.out.println(counter); 


            counter++; 


        } 

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

        } 

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

        } 

        public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
         // TODO Auto-generated method stub 
        } 
       }); 

        } 
         }, 0, 1000L); 
    */ 

    } 
    catch(Exception e) 
    { 

     e.printStackTrace(); 
    } 
} 
private void _startService() { 
    counter=0L; 
    timer.scheduleAtFixedRate(
      new TimerTask() { 
      public void run() { 

       counter++; 
       getLocatonUpdate(getApplicationContext()); 


      } 
      }, 
      0, 
      1000); 
     Log.i(getClass().getSimpleName(), "Timer started!!!"); 
    } 

@Override 
public void onDestroy() 
{ 
    super.onDestroy(); 
    // Cancel the persistent notification. 

    Toast.makeText(this, "Service destroyed at " + time.getTime() + "; counter is at: " + counter, Toast.LENGTH_LONG).show(); 

} 

public void getLocatonUpdate(Context c) 
{ 
    locationManager=(LocationManager)c.getSystemService(c.LOCATION_SERVICE); 
    locationManager.requestLocationUpdates("gps", (long)30000, (float) 10.0, new LocationListener() 
    { 
     public void onLocationChanged(Location arg0) 
     { 



       System.out.println(counter); 
       handleLocationChanged(arg0); 



     } 

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

    } 

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

    } 

    public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
    // TODO Auto-generated method stub 
    } 
    }); 



} 

private void handleLocationChanged(Location loc) 
{ 

     this.currentLocation = loc; 
     // Update the latitude & longitude TextViews 
     System.out.println(Double.toString(loc.getLatitude())); 
     System.out.println(Double.toString(loc.getLongitude())); 




    } 



} 

這是例外

 02-11 19:05:06.930: ERROR/AndroidRuntime(614): FATAL EXCEPTION: Timer-0 
     02-11 19:05:06.930: ERROR/AndroidRuntime(614): java.lang.RuntimeException: Can't  
     create handler inside thread that has not called Looper.prepare() 

     02-11 19:05:06.930: ERROR/AndroidRuntime(614):  at android.os.Handler.<init> 
     (Handler.java:121) 

     02-11 19:05:06.930: ERROR/AndroidRuntime(614):  at 
     android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:173) 

     02-11 19:05:06.930: ERROR/AndroidRuntime(614):  at 
     android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:173) 

    02-11 19:05:06.930: ERROR/AndroidRuntime(614):  at 
     android.location.LocationManager._requestLocationUpdates(LocationManager.java:579) 

    02-11 19:05:06.930: ERROR/AndroidRuntime(614):  at 
    android.location.LocationManager.requestLocationUpdates(LocationManager.java:446) 

    02-11 19:05:06.930: ERROR/AndroidRuntime(614):  at 
    com.excerciseTest.GpsService.getLocatonUpdate(GpsService.java:262) 
    02-11 19:05:06.930: ERROR/AndroidRuntime(614):  at 
    com.excerciseTest.GpsService$1.run(GpsService.java:239) 

    02-11 19:05:06.930: ERROR/AndroidRuntime(614):  at 
    java.util.Timer$TimerImpl.run(Timer.java:284) 

回答

0

當您創建多個線程並希望從其中一個線程更新UI時,通常會發生此錯誤。

您需要創建一個Handler以將來自不同線程的消息發送並捕獲到主UI線程(Activity類)中,以便使用您在該Activity類中定義的方法。

有關如何在線程中使用處理程序的示例,Android爲creating ProgressDialogs with a second thread提供了一個示例,如果您在該部分中存在「第二個線程的示例ProgressDialog」標題。

你必須在你的代碼中使用相同的意識形態。

1

你應該接012 中的getLocatonUpdate

異常正在升高,因爲LocationManager.requestLocationUpdates必須從Looper線程調用諸如主線程(見docs for LocationManager)。

計時器線程不是Looper線程。你可以做到這一點,但它不是必需的:requestLocationUpdates()已經定期通知你的回調。