等功能来运行,然后开始活动等功能

2023-09-12 04:33:07 作者:归途

当用户presses启动按钮,检查运行,看看GPS已启用,然后启动一个活性。checkGPS使用alertmanager。

When the users presses the start button , a check runs to see if GPS is enabled and then starts an activity.The checkGPS uses alertmanager.

case R.id.startbtn:
        checkGPS();
        Intent gpslocation=new Intent(this,selection.class);
        startActivity(gpslocation);
        break;

如何才能做到以等待用户启用GPS,然后启动活性?

How can I accomplish to wait to the user to enable GPS and then start the activity?

推荐答案

1)的onCreate()法,注册 LocationListener的您的活动:

1) in onCreate() method, register LocationListener on your Activity :

   // get location manager
   LocationManager lManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
   Button btnStart = (Button)findViewById(R.id.btnStart);
   btnStart.setOnClickListener(new OnClickListener() {
       @Override
       public void onClick(View v) {
            lManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, YourActivity.this);
       }
   });

2)你的活动应实施 LocationListener的

   public class YourActivity extends Activity implements LocationListener {

3)的替代方法 LocationListener的

@覆盖 公共无效onLocationChanged(位置定位){      //当位置发生了变化      Log.d(TAG的位置变了。); }

@Override public void onLocationChanged(Location location) { // when the position has changed Log.d(TAG, "location changed."); }

@Override
public void onProviderDisabled(String provider) {
     // when the source (GSP or GSM network) are disabled
     Log.d(TAG, "the source "+provider+" has been disabled");
}

@Override
public void onProviderEnabled(String provider) {
     Log.i(TAG, "the source "+provider+" has been enabled");
     //here you should test if the provider enabled is GPS , if yes , then launch your GPSActivity
     if(provider.equals("gps") {
           Intent gpslocation=new Intent(this,selection.class);
           startActivity(gpslocation);
     }
     else {
           //launch your alert dialog here
     }
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
     Log.i(TAG, "source status "+provider+" has been changed to : "+status);
}