基于GPS的Android应用报警应用

时间:2011-05-25 09:53:40

标签: android gps

我想创建一个类似的应用程序:

  1. 我在X市。
  2. 当我到达Y市时,我想收到通知(如响铃警报或振动等)。
  3. 我已经有sqlite数据库,其中包含许多城市的信息(纬度,经度)。

    我搜索了Google和SO但未找到合适的解决方案。

    到目前为止,我的研究表明:

    我要做的是创建ServiceBroadcastReceiver以使用GPS获取手机的当前位置。

    如果我出错了,请纠正我。

    我想以10分钟的间隔获得手机的当前位置。并且根据纬度经度值,我将其与预定城市纬度经度进行比较。

    如果匹配,则应该通知用户。

    我做过的事情:

    1. 我知道如何获取用户的当前位置。 (但不是通过创造服务。这就是我想知道的!)

    2. 我可以在sqlite数据库中添加任务。

    3. 请指导我如何处理此应用程序。

      当用户启动应用程序时启动它:

       Intent i = new Intent(context, MyLocationApps.class);     
       context.startService(i);
      

      public class MyLocationApps extends Service{
      
              LocationManager locMan;
              Location curLocation;
              Boolean locationChanged;
      
              LocationListener gpsListener = new LocationListener() {
              public void onLocationChanged(Location location) {
                      // Log.w("GPS", "Started");
                      if (curLocation == null) {
                              curLocation = location;
                              locationChanged = true;
                      }
      
                      if (curLocation.getLatitude() == location.getLatitude() && curLocation.getLongitude() == location.getLongitude())
                              locationChanged = false;
                      else
                              locationChanged = true;
      
                      curLocation = location;
      
                      if (locationChanged)
                              locMan.removeUpdates(gpsListener);
      
              }
      
              public void onProviderDisabled(String provider) {
      
              }
      
              public void onProviderEnabled(String provider) {
                      // Log.w("GPS", "Location changed", null);
              }
      
              public void onStatusChanged(String provider, int status,
                              Bundle extras) {
                      if (status == 0)// UnAvailable
                      {
      
                      } else if (status == 1)// Trying to Connect
                      {
      
                      } else if (status == 2) {// Available
      
                      }
              }
      
      };
      
      
             @Override
             public void onCreate() {
                     Toast.makeText(getBaseContext(),"Inside onCreate of Service", Toast.LENGTH_LONG).show();
      
                     Log.e(TAG, "Inside onCreate of Service");
                     super.onCreate();
      
                     locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                     locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,0, 0, gpsListener);
                     /*if (locMan.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                             locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,20000, 1, gpsListener);
                     } else {
                             this.startActivity(new Intent("android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS"));
                     }
      */
                     if (curLocation != null) {
                             double lat = curLocation.getLatitude();
                             double lng = curLocation.getLongitude();
                             Toast.makeText(getBaseContext(),"Lat : " + String.valueOf(lat) + "\n Long : "+ String.valueOf(lng), Toast.LENGTH_LONG).show();
      
                     }
                     else
                     {
                         Toast.makeText(getBaseContext(),"Didn Get any location", Toast.LENGTH_LONG).show();
                     }
             }
      
              final String TAG="LocationService";
              @Override
             public int onStartCommand(Intent intent, int flags, int startId) {
                       Toast.makeText(getBaseContext(),"Inside onStartCommand of Service", Toast.LENGTH_LONG).show();
                       Log.e(TAG, "Inside onStartCommand of Service");
      
      
      
                       return super.onStartCommand(intent, flags, startId);
             }
             @Override
             public void onLowMemory() {
                     // TODO Auto-generated method stub
                     super.onLowMemory();
             }
      
      
                @Override
                public void onStart(Intent i, int startId)
                {
                    Toast.makeText(getBaseContext(),"Inside onStart of Service", Toast.LENGTH_LONG).show();
                   Log.e(TAG, "Inside onStart of Service");
      
      
            }
      
                public IBinder onBind(Intent arg0) {
                          Log.e(TAG, "Inside onBind of Service");
                           return null;
                }
      }
      

      代码中的问题

      当我第一次运行应用程序时,它会显示Toast Messages。 但它永远不会显示Toast消息,即使我通过DDMS发送位置..

      我哪里出错你能告诉我吗?

3 个答案:

答案 0 :(得分:1)

在这里查看代码:android-mycycle/MyLocationManager.java

我已经为我的应用程序实现了这个。我在开始接收位置之前尝试获取gps修复。

答案 1 :(得分:1)

最后我得到了它的工作:(感谢Ganapathy& Farhan)

public class MyLocationApps extends Service{

LocationManager locMan;
Location curLocation;
Boolean locationChanged;

Handler handler = new Handler();

LocationListener gpsListener = new LocationListener() {
    public void onLocationChanged(Location location) {
            // Log.w("GPS", "Started");
            if (curLocation == null) {
                    curLocation = location;
                    locationChanged = true;
            }

            if (curLocation.getLatitude() == location.getLatitude() && curLocation.getLongitude() == location.getLongitude())
                    locationChanged = false;
            else
                    locationChanged = true;

            curLocation = location;

            if (locationChanged) 
                    locMan.removeUpdates(gpsListener);

    }

    public void onProviderDisabled(String provider) {

    }

    public void onProviderEnabled(String provider) {
            // Log.w("GPS", "Location changed", null);
    }

    public void onStatusChanged(String provider, int status,
                    Bundle extras) {
            if (status == 0)// UnAvailable
            {

            } else if (status == 1)// Trying to Connect
            {

            } else if (status == 2) {// Available

            }
    }

};


   @Override
   public void onCreate() {
           Toast.makeText(getBaseContext(),"Inside onCreate of Service", Toast.LENGTH_LONG).show();

           Log.e(TAG, "Inside onCreate of Service");
           super.onCreate();

           locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
           locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,0, 0, gpsListener);
           /*if (locMan.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                   locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,20000, 1, gpsListener);
           } else {
                   this.startActivity(new Intent("android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS"));
           }
 */
           if (curLocation != null) {
                   double lat = curLocation.getLatitude();
                   double lng = curLocation.getLongitude();
                   Toast.makeText(getBaseContext(),"Lat : " + String.valueOf(lat) + "\n Long : "+ String.valueOf(lng), Toast.LENGTH_LONG).show();

           }
           else
           {
               Toast.makeText(getBaseContext(),"Didn Get any location", Toast.LENGTH_LONG).show();
           }
   }

    final String TAG="LocationService";
    @Override
   public int onStartCommand(Intent intent, int flags, int startId) {
             Toast.makeText(getBaseContext(),"Inside onStartCommand of Service", Toast.LENGTH_LONG).show();
             Log.e(TAG, "Inside onStartCommand of Service");



             return super.onStartCommand(intent, flags, startId);
   }
   @Override
   public void onLowMemory() {
           // TODO Auto-generated method stub
           super.onLowMemory();
   }


  @Override
  public void onStart(Intent i, int startId)
  {
      Toast.makeText(getBaseContext(),"Inside onStart of Service", Toast.LENGTH_LONG).show();
     Log.e(TAG, "Inside onStart of Service");

     handler.postDelayed(GpsFinder,5000);// will start after 5 seconds
  }

  public IBinder onBind(Intent arg0) {
            Log.e(TAG, "Inside onBind of Service");
             return null;
  }
  public Runnable GpsFinder = new Runnable()
  {

    public void run()
    {
      // TODO Auto-generated method stub

           if (locMan.isProviderEnabled(LocationManager.GPS_PROVIDER))
            {

                locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,0, 0, gpsListener);
            }
            else
            {
                    getApplicationContext().startActivity(new Intent("android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS"));
            }

            if (curLocation != null) {
                    double lat = curLocation.getLatitude();
                    double lng = curLocation.getLongitude();
                    Toast.makeText(getBaseContext(),"Lat : " + String.valueOf(lat) + "\n Long : "+ String.valueOf(lng), Toast.LENGTH_LONG).show();

            }

            handler.postDelayed(GpsFinder,5000);// register again to start after 5 seconds...


    }
  };
 }

答案 2 :(得分:0)

我认为您应该使用Geofenceintentservice。有关详细信息,请参阅official documentation