2011-08-12 18 views

回答

0

那么,如果您使用LocationManager -class并根据onLocationChanged()采取行动,那么在获得某个职位之前它不会实际触发。

编辑:这是你可以尝试的东西。 编辑#2:哦,我其实误解了这个问题。尽管我会留下我的回应。

.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
> 
    <Button android:id="@+id/mybutton1" 
     android:visibility="INVISIBLE" 
     android:layout_width="200dp" 
     android:layout_height="100dp" 
     android:text="MyButton1" 
    /> 
</LinearLayout> 

.java:

//Get the LocationManager & Button 
final LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
final Button button = (Button) findViewById(R.id.mybutton1); 

//Add the listener 
LocationListener locationListener = new LocationListener() { 

    public void onLocationChanged(Location location) { 

     //Remove the listener and make the button visible   
     locManager.removeUpdates(locationListener); 
     button.setVisibility(1); 
    } 

    public void onStatusChanged(String provider, int status, Bundle extras) {} 
    public void onProviderEnabled(String provider) {} 
    public void onProviderDisabled(String provider) {} 
}; 

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); 

让我知道这是否为你!

+0

是否可以设置服务的按钮可见性? LocationListener是应用程序中服务的一部分。谢谢。 – danny

+0

我对此不太确定,但是您能否通过变量将其传递给您的应用程序,并检查启动活动?这将是一个静态解决方案。 – ninetwozero

-1

你可以使用这个代码在你的按钮点击检查GPS的状态。

LocationListener ll = new MyLocationListener(); 
    lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, ll); 
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, ll); 

    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

     Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 


     if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 

      Log.i("About GPS", "GPS is Enabled in your devide"); 
      AlertDialog.Builder GPSON =new Builder(MyCardio.this); 
      GPSON.setCancelable(false); 
      GPSON.setMessage("GPS IS ON"); 
      GPSON.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 

       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        // TODO Auto-generated method stub 

       } 
      }); 
      GPSON.show();   
     } else { 

      AlertDialog.Builder GPSOFF = new Builder(MyCardio.this); 
      GPSOFF.setCancelable(false); 
      GPSOFF.setTitle("Connection Error"); 
      GPSOFF.setMessage(" Please Enable Your GPS !"); 
      GPSOFF.setPositiveButton("Ok",new DialogInterface.OnClickListener() 
       { 
        public void onClick(DialogInterface dialog,int which) 
        { 


        // startActivity(new Intent(LoginActivity.this,MainMenuActivity.class)); 
        } 
       }); 
      GPSOFF.show(); 
        } 
+0

我不认为这会检查以确定是否存在GPS定位 - 只能检查GPS接收器是否启用。 GPS可以在没有适当修复的情况下启用。另外,getLastKnownLocation不能确保“当前gps会话”的位置。如果您的GPS接收器位于地下1000米处,此代码将返回true。 – Ian