当用户按下开始按钮时,会运行检查以查看 GPS 是否已启用,然后启动活动。checkGPS 使用警报管理器。
case R.id.startbtn:
checkGPS();
Intent gpslocation=new Intent(this,selection.class);
startActivity(gpslocation);
break;
如何完成等待用户启用 GPS 然后开始活动?
当用户按下开始按钮时,会运行检查以查看 GPS 是否已启用,然后启动活动。checkGPS 使用警报管理器。
case R.id.startbtn:
checkGPS();
Intent gpslocation=new Intent(this,selection.class);
startActivity(gpslocation);
break;
如何完成等待用户启用 GPS 然后开始活动?
1)在onCreate()
方法中,LocationListener
在您的 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
:
@Override public void onLocationChanged(Location location) { // 当位置改变时 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);
}