2013-10-16 36 views
1

我已经做了一个android应用程序,它有助于在A-GPS的帮助下找到GPS位置,打开GPS设置后,我想运行进度对话框直到经度和纬度都大于零。我是一个新手,我写了下面的代码不能正常工作。 任何帮助将不胜感激运行进度对话框,直到Android应用程序中找不到GPS ID

感谢

package com.example.gpsutility; 



    import android.os.Bundle; 
    import android.app.Activity; 
    import android.app.ProgressDialog; 
    import android.util.Log; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.widget.Button; 
    import android.widget.TextView; 
    import android.widget.Toast; 

     public class GPSActivity extends Activity implements OnClickListener { 
Button search,exit; 
TextView location_tv; 
GpsService gps; 
ProgressDialog progressDialog = null; 
double latitude,longitude; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_gps); 
    search = (Button)findViewById(R.id.search_button); 
    exit = (Button)findViewById(R.id.exit_button); 
    location_tv =(TextView)findViewById(R.id.textview2); 
    gps = new GpsService(this); 
    search.setOnClickListener(this); 
    exit.setOnClickListener(this); 
} 

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    if(v==search) 
    {  
     progressDialog = ProgressDialog.show(this, "", "getting location...", true); 

      new Thread(new Runnable() { 
         public void run() { 
          runOnUiThread(new Runnable() { 
            public void run() { 
      if(latitude<=0 && longitude<=0) 
      { 
       do{ 
        latitude =gps.getLatitude(); 
        longitude = gps.getLongitude(); 
      //  location_tv.setText("Location is -\nLat: "+latitude+ "\nLong: "+longitude); 
       }while(latitude<=0 && longitude<=0); 

       location_tv.setText("  Location is-\nLat:"+latitude+"\nLong: "+longitude);  
        progressDialog.dismiss(); 


      }else{ 
       location_tv.setText("  Location is-\nLat:"+latitude+"\nLong: "+longitude);  
       Log.e("latlong", latitude + ""+longitude); 
        progressDialog.dismiss(); 

      } 
            } 
           });      


             } 

         }).start(); } 

if(v==exit) 
    { 
     finish(); 
    } 
} 




} 

我使用的GPS服务类是

package com.example.gpsutility; 



    public class GpsService extends Service implements LocationListener { 

private final Context mContext; 
    boolean isGPSEnabled = false; 
    boolean isNetworkEnabled = false; 
    boolean canGetLocation = false; 
    Location location; 
    double latitude; 
    double longitude; 
    public static final long MIN_DISTANCE_CHANGE_FOR_UPDATES=10; 
    public static final long MIN_TIME_BW_UPDATES=1000*60*1; 
    protected LocationManager locationManager; 


    public GpsService(Context context) 
    { 
     this.mContext=context; 
     getLocation(); 
    } 
    @Override 
    public void onLocationChanged(Location location) { 
     // TODO Auto-generated method stub 

    } 

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

    } 

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

    } 

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

    } 

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

    public Location getLocation() 
    { 
     try { 
      locationManager =(LocationManager) 

mContext.getSystemService(LOCATION_SERVICE); isGPSEnabled = locationManager.isProviderEnabled(locationManager.GPS_PROVIDER);

  isNetworkEnabled = locationManager.isProviderEnabled(locationManager.NETWORK_PROVIDER); 

      if(!isGPSEnabled || !isNetworkEnabled) 
      { 
       showSettingsAlert(); 
      } 
      else { 
       this.canGetLocation=true; 
       if(isNetworkEnabled) 
       { 
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 
          MIN_TIME_BW_UPDATES, 
          MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
        Log.d("Network", "Network"); 
        if (locationManager != null) { 
         location = locationManager 
           .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
         if (location != null) { 
          latitude = location.getLatitude(); 
          longitude = location.getLongitude(); 
         } 
       } 
       } 
       if (isGPSEnabled) { 
        if (location == null) { 
         locationManager.requestLocationUpdates(
           LocationManager.GPS_PROVIDER, 
           MIN_TIME_BW_UPDATES, 
           MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
         Log.d("GPS Enabled", "GPS Enabled"); 
         if (locationManager != null) { 
          location = locationManager 
            .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
          if (location != null) { 
           latitude = location.getLatitude(); 
           longitude = location.getLongitude(); 
          } 
         } 
        } 
       } 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return location; 
    } 


    public double getLatitude(){ 
      if(location != null){ 
       latitude = location.getLatitude(); 
      } 

      // return latitude 
      return latitude; 
     } 

     /** 
     * Function to get longitude 
     * */ 
     public double getLongitude(){ 
      if(location != null){ 
       longitude = location.getLongitude(); 
      } 

      // return longitude 
      return longitude; 
    } 
     public boolean canGetLocation() { 
      return this.canGetLocation; 
     } 

     public void showSettingsAlert(){ 
      AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext); 

      // Setting Dialog Title 
      alertDialog.setTitle("GPS settings"); 

      // Setting Dialog Message 
      alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?"); 

      // Setting Icon to Dialog 
      //alertDialog.setIcon(R.drawable.delete); 

      // On pressing Settings button 
      alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog,int which) { 
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
        mContext.startActivity(intent); 
       } 
      }); 

      // on pressing cancel button 
      alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
       dialog.cancel(); 
       } 
      }); 

      // Showing Alert Message 
      alertDialog.show(); 
     } 



     } 

回答

2

试试这个。

ProgressDialog progressBar; 
int progressStatus = 0; 

progressBar = new ProgressDialog(this); 
progressBar.setCancelable(false); 
progressBar.setMessage("getting location..."); 
progressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
progressBar.setProgress(0); 
progressBar.setMax(100); 
progressBar.show(); 
progressBarStatus = 0; 


new Thread(new Runnable() { 
      public void run() { 
      while (progressBarStatus < 100) { 

       progressBarStatus = getLocation(); 

       try { 
       Thread.sleep(1000); 
       } catch (InterruptedException e) { 
       e.printStackTrace(); 
       } 

       progressBarHandler.post(new Runnable() { 
       public void run() { 
        progressBar.setProgress(progressBarStatus); 
       } 
       }); 
      } 

      if (progressBarStatus >= 100) { 
       try { 
        Thread.sleep(2000); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
       progressBar.dismiss(); 
      } 
      } 
      }).start(); 


private int getLocation() 
{ 
    GpsService gps = new GpsService(this) 
    latitude = gps.getLatitude(); 
    longitude = gps.getLongitude(); 

    if(latitude > 0 && longitude > 0) 
     return 100; 
    else 
     return 0; 

} 

此外,你确定你需要使用它作为服务?或者那只是一个名字而已,它真的只是一个简单的课程?如果它是一个简单的课程,并且您只是通过按钮或任何操作拉动位置,则可以使用上述代码。因为当你在android中声明Service时,你在后台运行它,并且如果要在X间隔后重复执行进程,并且在通过代码停止或使用stopSelf()时停止,则需要创建一个处理程序。如果它确实是一项服务,则需要为您的活动创建一个广播侦听器,并根据广播使用该服务发送广播并更新您的UI。我希望这有帮助。干杯!

+0

@Ijpv谢谢,我刚刚将我的类命名为GPSService,但我不使用作为服务它是一个活动。 – user2834664

+0

这里progressBarHandler是什么? – user2834664

+0

它是一个通用处理程序。创建一个全局变量Handler progressBarHandler = new Handler(); – ljpv14

相关问题