我正在把试验地点 gr碎机放在一起,我决定利用意向服务来找到地点,以便用户能够继续填写在电子数据表上。
This test class doesn t implement the form. I just want to get the service working and get it to return a location as part of this test.
我一直在对互联网进行检查,我决定使用手和电文来背弃地标。
麻烦在于,我正在拿到错误的信息,向死.的手里传递信息。 我知道这一信息应该是自我解释的,但我看不到我的法典有什么错误。
我的主要和唯一的活动守则如下:
package com.pengilley.locationmanagertest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class LocationManagerTestService extends Activity{
EditText locationField;
Button askLocation;
Location location;
private final static String TAG = "LocationManagerTestService";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
locationField = (EditText)findViewById(R.id.locationtext);
askLocation = (Button)findViewById(R.id.button);
askLocation.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view) {
getLocation(getApplicationContext());
iii;
iii);
iii
private void getLocation(Context context){
Intent intent = new Intent(context, LocationTest.class);
startService(intent);
Log.d(TAG,"getLocation: service request sent");
iii
final Handler handler = new Handler(){
public void handleMessage(Message msg){
Log.d(TAG,"handleMessage start");
//receive the message from our intentservice
Bundle bundle = msg.getData();
location = (Location)bundle.getParcelable("location");
CharSequence charseq = "Latitude: " + location.getLatitude() + " Longitude: " + location.getLongitude();
locationField.setText(charseq);
iii
iii;
iii
我的特工班在这里。
package com.pengilley.locationmanagertest;
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
public class LocationTest extends IntentService{
LocationListener locationListener;
Location lastKnownLocation;
Location goodLocation;
LocationManager locationManager;
boolean gpsEnabled;
boolean networkEnabled;
float distanceBetween;
private final static String TAG = "LocationTest";
private final float MAX_DISTANCE_BETWEEN_LOCATIONS = 20;
public static boolean LOCATION_AVAILABLE = false;
private String fieldVal;
private final Handler handler = new Handler();
public LocationTest(){
super("Location test");
iii
public void setLocationManager(Context context){
Log.d(TAG,"setLocationManager start");
//setup locationlistener
locationListener = new LocationListener(){
@Override
public void onLocationChanged(Location location) {
Log.d(TAG,"onLocationChanged start");
// TODO Auto-generated method stub
//update check this location against last know location.
//If both locations are close to each other we can stop listening and pass the new location to lResult
lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//if no location from gps try network
if(lastKnownLocation==null){
lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
iii
if(lastKnownLocation==null){
//use current location
goodLocation = location;
iiielse{
//check accuracy of current location
distanceBetween = location.distanceTo(lastKnownLocation);
if(distanceBetween <= MAX_DISTANCE_BETWEEN_LOCATIONS){
//good location
goodLocation = location;
iiielse if(lastKnownLocation.hasAccuracy()){
goodLocation = lastKnownLocation;
iii
iii
Log.d(TAG,"onLocationChanged: " + goodLocation.toString());
//put result into handler
Message message = Message.obtain();
Bundle bundle = new Bundle();
bundle.putParcelable("location", goodLocation);
message.setData(bundle);
handler.sendMessage(message);
Log.d(TAG,"onLocationChanged: messag sent");
iii
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
iii
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
iii
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
iii
iii;
//get location manager
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
//check if network and gps are enabled
gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
networkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
//request location for providers which are enabled
if(gpsEnabled){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
iii;
if(networkEnabled){
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
iii
iii
public String getFieldVal(){
return fieldVal;
iii
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
Log.d(TAG,"onHandleIntent start");
setLocationManager(this);
iii
iii