我已经使用了这段代码并且正在运作:
if(MyLocationListener.latitude>0){
MyToast.makeText(Weather.this,Double.toString(MyLocationListener.latitude));
MyToast.makeText(Weather.this, Double.toString(MyLocationListener.longitude));
lat=MyLocationListener.latitude;
lon=MyLocationListener.longitude;
Log.v("this","lat "+Double.toString(lat) );
Log.v("this","lon "+Double.toString(lon) );
}
我需要在连接gps时调用一个类。上面的代码在一个按钮中,因此用户每次都应该点击按钮以了解gps是否已连接!!
如何在连接gps时调用类?
谢谢
答案 0 :(得分:4)
此代码可能会对您有所帮助
将ACCESS_FINE_LOCATION权限添加到您的清单 获取系统LocationManager的实例 创建一个对GPS_EVENT_SATELLITE_STATUS作出反应的GpsStatus.Listener 使用带有addGpsStatusListener
的LocationManager注册侦听器GpsStatus.Listener listener = new GpsStatus.Listener() { void onGpsStatusChanged(int事件) {
if (event == GPS_EVENT_SATELLITE_STATUS)
{
GpsStatus status = mLocManager.getGpsStatus(null);
Iterable<GpsSatellite> sats = status.getSatellites();
// Check number of satellites in list to determine fix state
}
}
}
答案 1 :(得分:0)
我认为这段代码可以帮助你解决问题
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
以上是你必须在manifest.xml中添加的内容
以下是代码
public class ExampleApp extends Activity {
/** Called when the activity is first created. */
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
Toast.makeText(this, "GPS is Enabled in your devide", Toast.LENGTH_SHORT).show();
}else{
showGPSDisabledAlertToUser();
}
}
private void showGPSDisabledAlertToUser(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("GPS is disabled in your device. Would you like to enable it?")
.setCancelable(false)
.setPositiveButton("Goto Settings Page To Enable GPS",
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
Intent callGPSSettingIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(callGPSSettingIntent);
}
});
alertDialogBuilder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
dialog.cancel();
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
}
答案 2 :(得分:0)
当gps连接时,你必须使用BroadCastRecievers来通知..
Gps status enabled/disabled broadcast receiver
这肯定会帮助您如何使用广播进行gps状态更改。
答案 3 :(得分:0)
在Manifest
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
在您的活动中尝试此操作:
LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
buildAlertMessageNoGps();
}
private void buildAlertMessageNoGps() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}