我正在尝试运行用户选择时间间隔的服务,此时 GPS 位置正在更新。
我的应用不更新位置。
我让谷歌地图工作的选择类(以前工作过):
public class selection extends FragmentActivity implements OnMyLocationChangeListener{
GoogleMap googleMap;
double latitude=0.0;
double longitude=0.0;
String code_value="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.selection);
// Getting Google Play availability status
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
// Showing status
if....
// Getting reference to the SupportMapFragment of activity_main.xml
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
....
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId == R.id.rb_normal){
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
....
else if(checkedId == R.id.save_location){
new Thread(new Runnable() {
public void run() {
//retrieve this value from configure class
code_value=getIntent().getStringExtra("code_value");
postData(Double.toString(latitude),Double.toString(longitude),code_value);
}
}).start();
}
...
@Override
public void onMyLocationChange(Location location) {
TextView tvLocation = (TextView) findViewById(R.id.tv_location);
// Getting latitude of the current location
latitude = location.getLatitude();
// Getting longitude of the current location
longitude = location.getLongitude();
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
...
}
public void postData(String lat, String lo ,String code) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpGet htget = new HttpGet("www.test.com"+lat+"/"+lo+"/"+code);
try {
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(htget);
String resp = response.getStatusLine().toString();
Toast.makeText(this, resp, 5000).show();
...
}
我使用 alarmmanager 更新位置的 myservice 类。
public class myservice extends IntentService {
private Context mContext;
private Handler mHandler;
public myservice(String name) {
super(name);
}
// call from startService()
public myservice() {
super("MyService");
mHandler = new Handler();
}
@Override
protected void onHandleIntent(Intent intent) {
String time_value;
time_value=(String) intent.getStringExtra("time_value");
final long thetime=Long.parseLong(time_value);
mContext = getApplicationContext();
mHandler.post(new Runnable(){
@Override
public void run() {
// Start service using AlarmManager
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 10);
Intent intent = new Intent(myservice.this,selection.class);
PendingIntent pintent = PendingIntent.getService(myservice.this, 0, intent,
0);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
thetime*1000, pintent);
// Tell the user about what we did.
Toast.makeText(myservice.this, "Configured time interval",
Toast.LENGTH_LONG).show();
}
});
}
}
我启动服务的配置类:
public void onClick(View v) {
switch (v.getId()){
case R.id.btn:
edit11=edit1.getText().toString();
edit22=edit2.getText().toString();
Intent i=new Intent(this,selection.class);
i.putExtra("code_value",edit11);
Intent k=new Intent(this,myservice.class);
k.putExtra("time_value",edit22);
this.startService(k);
Intent l=new Intent(this,MainActivity.class);
startActivity(l);
break;
}