这就是我想要做的。 我有主要Activity调用BroadcastReceiver名称SmsAlarmReceiver。如何在Android的IntentService中获取用户的当前位置?
Intent i = new Intent(SmsAlarmReceiver.ALARM_ACTION);
sendBroadcast(i);
现在我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);
}
}
所以在这种接收器,我请求使用的PendingIntent听位置改变位置更新。
我叫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);
}
}
我的应用程序不强制关闭,但它不能发送到接收器指定的消息。
有没有更好的替代这个问题。简而言之,我想实现每5分钟发送一次用户GPS位置的功能,一旦用户启用了跟踪功能。我可以如何使用服务/意图服务/广播接收器来实现此功能。 ?请帮助..
我已经通过它走了,但作为一个在的Java/Android开发我没有得到写在代码中的新手repo.Is有任何其他的方式,我可以工作了这一点?> – Droidwala
我知道的基本知识,但我要问的是,为什么requestlocationupdates不在服务工作,他们在活动的工作方式.. – Droidwala
我终于明白与实施it.Its工作!感谢 – Droidwala