问题描述
|
我想创建一个像这样的应用程序:
我在X城市
当我到达城市Y时,我想收到通知(例如响铃警报或振动等)。
我已经有sqlite数据库,其中包含有关许多城市的信息(纬度,经度)。
我已经搜索过Google和SO,但是找不到合适的解决方案。
到目前为止,我的研究表明:
我要做的是创建一个
Service
或BroadcastReceiver
,以使用GPS获取手机的当前位置。
如果我做错了,请纠正我。
我想每隔10分钟获取一次手机的当前位置。基于该纬度经度值,我将其与计划的城市纬度经度进行比较。
如果发生匹配,则应通知用户。
我所做的事情:
我知道如何获取用户的当前位置。 (但不是通过创建服务。这就是我想知道的!)
我能够在sqlite数据库中添加任务。
请指导我如何处理该应用程序。
在用户启动应用程序时启动它:
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,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消息。
但是,即使我通过DDMS发送位置,它也不会显示Toast消息。
我要去哪里了,你能告诉我吗?
解决方法
在这里看看代码:android-mycycle / MyLocationManager.java
我已经为我的应用程序实现了这一点。开始接收位置之前,我试图获取gps修复程序。
,终于我开始工作了:(感谢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,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,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(),Toast.LENGTH_LONG).show();
}
handler.postDelayed(GpsFinder,5000);// register again to start after 5 seconds...
}
};
}
,我认为您应该同时使用Geofence
和intentservice
。有关更多信息,请参考官方文档。