public class Gpstracker extends Service implements LocationListener {
@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Disabled"+arg0, 10000).show();
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Enabled"+arg0, 10000).show();
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
这是我的GpsTracker课程:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Gpstracker gps=new Gpstracker();
}
}
这是MainActivity:当我启用wifi或Gps时,如果我们禁用无法显示你的wifi关闭的不显示toast消息,那么它不会显示toast一样请帮助我们检查位置是启用还是禁用。 请告诉我如何检测使用LocationLisner打开或关闭的服务来显示
答案 0 :(得分:0)
要使LocationListener
生效,您需要按以下方式request
位置更新:
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, gps); //time and distance as per your requirement. gps is your listener class object
当应用程序要销毁或在后台运行时,您可以使用removeLocationUpdates
取消注册侦听器。取决于要求。
lm.removeUpdates(gps);
<强> BUT 强>
您说您只是想要启用或禁用GPS通知。所以我建议你采用BroadcastReceiver方法。
在清单中,注册接收者:
<receiver android:name="com.pkg.MyReceiver">
<intent-filter>
<action android:name="android.location.PROVIDERS_CHANGED" />
</intent-filter>
</receiver>
您的MyReceiver类:
public class MyReceiver extends BroadcastReceiver
{
@Override
public void onReceive( Context context, Intent intent )
{
final LocationManager manager = (LocationManager) context.getSystemService( Context.LOCATION_SERVICE );
if (manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
//Show toast of enabled
}
else
{
//Show toast of disabled
}
}
}
如果您只想在应用程序位于前台时进行更新,请在活动的onResume
中注册,然后在onPause
中取消注册
希望这有帮助。