本文介绍了等功能来运行,然后开始活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当用户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的位置变了。);
}
@Overridepublic 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);
}
这篇关于等功能来运行,然后开始活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!