NotifyActivity.java 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package com.baidu.baidulocationdemo;
  2. import com.baidu.location.BDLocation;
  3. import com.baidu.location.BDLocationListener;
  4. import com.baidu.location.BDNotifyListener;
  5. import com.baidu.location.LocationClient;
  6. import android.app.Activity;
  7. import android.app.Service;
  8. import android.os.Bundle;
  9. import android.os.Handler;
  10. import android.os.Message;
  11. import android.os.Vibrator;
  12. import android.view.View;
  13. import android.view.View.OnClickListener;
  14. import android.widget.Button;
  15. import android.widget.Toast;
  16. public class NotifyActivity extends Activity{
  17. private Button startNotify;
  18. private Vibrator mVibrator;
  19. private LocationClient mLocationClient;
  20. private NotiftLocationListener listener;
  21. private double longtitude,latitude;
  22. private NotifyLister mNotifyLister;
  23. @Override
  24. protected void onCreate(Bundle savedInstanceState) {
  25. // TODO Auto-generated method stub
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.notify);
  28. listener = new NotiftLocationListener();
  29. mVibrator =(Vibrator)getApplicationContext().getSystemService(Service.VIBRATOR_SERVICE);
  30. startNotify = (Button)findViewById(R.id.notifystart);
  31. mLocationClient = new LocationClient(this);
  32. mLocationClient.registerLocationListener(listener);
  33. startNotify.setOnClickListener(new OnClickListener() {
  34. @Override
  35. public void onClick(View v) {
  36. // TODO Auto-generated method stub
  37. if(startNotify.getText().toString().equals("开启位置提醒")){
  38. mLocationClient.start();
  39. startNotify.setText("关闭位置提醒");
  40. }else{
  41. if(mNotifyLister!=null){
  42. mLocationClient.removeNotifyEvent(mNotifyLister);
  43. startNotify.setText("开启位置提醒");
  44. }
  45. }
  46. }
  47. });
  48. }
  49. @Override
  50. protected void onStop() {
  51. // TODO Auto-generated method stub
  52. super.onStop();
  53. mLocationClient.removeNotifyEvent(mNotifyLister);
  54. mLocationClient = null;
  55. mNotifyLister= null;
  56. listener = null;
  57. }
  58. private Handler notifyHandler = new Handler(){
  59. @Override
  60. public void handleMessage(Message msg) {
  61. // TODO Auto-generated method stub
  62. super.handleMessage(msg);
  63. mNotifyLister = new NotifyLister();
  64. mNotifyLister.SetNotifyLocation(latitude,longtitude, 3000,"gcj02");//4个参数代表要位置提醒的点的坐标,具体含义依次为:纬度,经度,距离范围,坐标系类型(gcj02,gps,bd09,bd09ll)
  65. mLocationClient.registerNotify(mNotifyLister);
  66. }
  67. };
  68. public class NotiftLocationListener implements BDLocationListener {
  69. @Override
  70. public void onReceiveLocation(BDLocation location) {
  71. //Receive Location
  72. longtitude = location.getLongitude();
  73. latitude = location.getLatitude();
  74. notifyHandler.sendEmptyMessage(0);
  75. }
  76. }
  77. public class NotifyLister extends BDNotifyListener{
  78. public void onNotify(BDLocation mlocation, float distance){
  79. mVibrator.vibrate(1000);//振动提醒已到设定位置附近
  80. Toast.makeText(NotifyActivity.this, "震动提醒", Toast.LENGTH_SHORT).show();
  81. }
  82. }
  83. }