[英]how to get current location of user in IntentService in Android?
This what i'm trying to do. 这就是我想做的。 I have main Activity calling BroadcastReceiver name SmsAlarmReceiver.
我有一个主要的Activity,名为BroadcastReceiver,名称为SmsAlarmReceiver。
Intent i = new Intent(SmsAlarmReceiver.ALARM_ACTION);
sendBroadcast(i);
Now my SmsAlarmReceiver.java looks like: 现在我的SmsAlarmReceiver.java看起来像:
public class SmsAlarmReceiver extends BroadcastReceiver {
LocationManager locationmanager;
public static final String ALARM_ACTION= "com.example.finaltracking.SMS_REC";
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
locationmanager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Intent smsintent = new Intent(context,SmsService.class);
PendingIntent pendingintent = PendingIntent.getService(context, 0, smsintent, 0);
locationmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1*60*1000,10,pendingintent);
}
} }
So in this receiver i am requesting for location updates using pendingIntent to listen to location change. 因此,在此接收器中,我正在请求使用endingIntent来侦听位置更改的位置更新。
My service named SmsService.java looks like: 我的名为SmsService.java的服务如下所示:
public class SmsService extends IntentService {
public SmsService(String name) {
super(name);
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
Bundle bundle = intent.getExtras();
Location location = (Location) bundle.get(LocationManager.KEY_LOCATION_CHANGED);
Log.d("msg", "Loc is " + location.getLatitude() +","+ location.getLongitude());
sendMessage("983******","msg is " + location.getLatitude()+"," +location.getLongitude());
}
private void sendMessage(String rec,String msg){
//PendingIntent intent = PendingIntent.getActivity(this,0,new Intent(this,MainActivity.class),0);
SmsManager sms = SmsManager.getDefault();
//Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_LONG).show();
ArrayList<String> parts = sms.divideMessage(msg);
sms.sendMultipartTextMessage(rec,null, parts,null, null);
}
}
My app is not force closing but it is not able to send the message to receiver specified. 我的应用未强制关闭,但无法将消息发送到指定的接收者。
Is there any better alternative to this problem.In short i want to implement feature that sends GPS location of User every 5 minutes once user has enabled tracking feature.How can i implement this using services/Intent Services/Broadcast Receivers.?Please help.. 有没有更好的解决方案,总之,我想实现一种功能,一旦用户启用了跟踪功能,便每5分钟发送一次用户的GPS位置。如何使用服务/意图服务/广播接收器来实现此功能。 。
There is something called CWAC Location Poller: Finding Your Location Periodically 有一种叫CWAC的位置轮询器:定期查找您的位置
and improved version of it https://github.com/alexbirkett/cwac-locpoll 和它的改进版本https://github.com/alexbirkett/cwac-locpoll
Some simpler version 一些更简单的版本
http://developer.android.com/training/location/retrieve-current.html http://developer.android.com/training/location/retrieve-current.html
http://javapapers.com/android/get-current-location-in-android/ http://javapapers.com/android/get-current-location-in-android/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.