【发布时间】:2012-04-29 19:01:52
【问题描述】:
我正在尝试构建一个在用户靠近某个位置时启动通知的代码。我用谷歌搜索了这个主题,但找不到解决方案。我正在使用以下代码,但它不起作用。
下面的代码是我的Location类,它的内部类是MyLocationListener,它实现了LocationListener
` @Override
public void onLocationChanged(Location loc)
{
CharSequence from = "AlarmManager - 时间到了!";
CharSequence message = "这是您的警报";
loc.getLatitude();
loc.getLongitude();
String Text = "My current location is: " +
"Latitud = " + loc.getLatitude() +
"Longitud = " + loc.getLongitude();
float radius = 500; //Distance in meters
Location location1 = new Location("GPS");
location1.setLatitude(loc.getLatitude());
location1.setLongitude(location1.getLongitude());
Location location2 = new Location("GPS");
location2.setLatitude(22.727739);
location2.setLongitude(75.886074);
Float distance = location1.distanceTo(location2);
distance = Math.abs(distance);
if(distance < radius)
{
Log.d("sdsd", "task location");
notMan = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification n = new Notification(R.drawable.ic_launcher, "Click here for details", System.currentTimeMillis());
Intent notificationIntent = new Intent(Location_Gps.this,Location_Gps.class);
PendingIntent pendingIntent = PendingIntent.getActivity(Location_Gps.this, 0, notificationIntent, 0);
n.setLatestEventInfo(Location_Gps.this, from,message, pendingIntent);
notMan.notify(10001, n);
}
Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
}
感谢有关该主题的任何帮助。
【问题讨论】:
-
这是否在服务中运行?
-
不,它没有在服务内部运行