2013-04-11 26 views
1

以下是我从其他工作的编码。我正在尝试通过在我的应用程序中进行必要的更改来实现他的一些工作。这里的问题是,当单击GET ADDRESS按钮时,它不会返回正确的地址。此外,它仅适用于WiFi /移动网络,但不适用于GPS。与此同时,我想知道如何在打电话给联系人时自动发布数据。谢谢!不正确的位置更新与WiFi /移动网络,但不与GPS

package com.yang.address; 

import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Locale; 

import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 

import android.app.Activity; 
import android.content.Context; 
import android.location.Address; 
import android.location.Geocoder; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

public class AddressActivity extends Activity { 
    /** Called when the activity is first created. */ 
    Double lat, lon; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     Button btnLocation = (Button)findViewById(R.id.btnLocation); 
     btnLocation.setOnClickListener(new OnClickListener() { 
      public void onClick(View arg0) { 
       // Acquire a reference to the system Location Manager 
       LocationManager locationManager = (LocationManager) AddressActivity.this.getSystemService(Context.LOCATION_SERVICE); 
       // Define a listener that responds to location updates 
       LocationListener locationListener = new LocationListener() { 
        public void onLocationChanged(Location location) { 
         // Called when a new location is found by the network location provider. 
         lat = location.getLatitude(); 
         lon = location.getLongitude(); 
         TextView tv = (TextView) findViewById(R.id.txtLoc); 
         tv.setText("Your Location is:" + lat + "--" + lon); 
        } 

        public void onStatusChanged(String provider, int status, Bundle extras) {} 
        public void onProviderEnabled(String provider) {} 
        public void onProviderDisabled(String provider) {} 
       }; 
       // Register the listener with the Location Manager to receive location updates 
       locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 
      } 
     }); 

     Button btnSend = (Button)findViewById(R.id.btnSend); 
     btnSend.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       postData(lat, lon); 
      } 
     }); 

     Button btnAdd = (Button)findViewById(R.id.btnAddress); 
     btnAdd.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       TextView tv = (TextView)findViewById(R.id.txtAddress); 
       tv.setText(GetAddress(lat, lon)); 
      } 
     }); 

    } 

    public void postData(Double lat2, Double lon2) { 
     // Create a new HttpClient and Post Header 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpGet htget = new HttpGet("http://myappurl.com/Home/Book/"+lat2+"/"+lon2); 

     try { 
      // Execute HTTP Post Request 
      HttpResponse response = httpclient.execute(htget); 
      String resp = response.getStatusLine().toString(); 
      Toast.makeText(this, resp, 5000).show(); 


     } catch (ClientProtocolException e) { 
      Toast.makeText(this, "Error", 5000).show(); 
     } catch (IOException e) { 
      Toast.makeText(this, "Error", 5000).show(); 
     } 
    } 
    public String GetAddress(Double lat2, Double lon2) 
    { 
     Geocoder geocoder = new Geocoder(this, Locale.ENGLISH); 
     String ret = ""; 
     try { 
      List<Address> addresses = geocoder.getFromLocation(lat2,lon2, 1); 
      if(addresses != null) { 
       Address returnedAddress = addresses.get(0); 
       StringBuilder strReturnedAddress = new StringBuilder("Address:\n"); 
       for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) { 
        strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n"); 
       } 
       ret = strReturnedAddress.toString(); 
      } 
      else{ 
       ret = "No Address returned!"; 
      } 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      ret = "Can't get Address!"; 
     } 
     return ret; 
    } 
} 

回答

0

看起来你自从你上一个问题以来取得了一些进展。

首先,androids上的LocationManager系统是Listening Service,因为您将其注册为侦听器,并允许操作系统在更新完成时通知操作系统。这意味着您应该在之前将OnClick方法运行的代码放在之前。您最好的选择是让活动的OnCreate方法在活动启动时注册侦听器,并使用onLocationChanged回调来将新的Location存储在类变量上。

private Location lastKnownLocation; 
private TextView tv; 

public void onCreate(Bundle savedInstanceState) { 
    //.... 
    setupLocationServices(); 
    //.... 
    tv = (TextView) findViewById(R.id.txtLoc); 
    Button btnLocation = (Button)findViewById(R.id.btnLocation); 
    btnLocation.setOnClickListener(new OnClickListener() { 
    public void onClick(View arg0) { 
     updateLocation(); 
    } 
    }); 
    //.... 
} 

private void setupLocationServices() { 
    //.... 
    LocationListener locationListener = new LocationListener() { 
    public void onLocationChanged(Location location) { 
    // Called when a new location is found by the network location provider. 
    lastKnownLocation = location; 
    updateLocation(); 
    } 
    .... 
} 

private void updateLocation() { 
    double lat = lastKnownLocation.getLatitude(); 
    double lat = lastKnownLocation.getLongitude(); 
    tv.setText("Your Location is:" + lat + "--" + lon); 
} 

至于你的理由,为什么它不能与GPS一起工作,这是因为你从来没有注册GPS监听器。当你有这样一行:

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 

您需要在此值可分别代替:

LocationManager.GPS_PROVIDER 

再次,我建议之前,我真的建议抽出宝贵时间来充分了解Android Developer documentation

至于有关收听来电状态的其他问题,您应该首先检查TelephonyManager文档here。特别注意使用ACTION_PHONE_STATE_CHANGED及其额外的来电。对于拨出电话谷歌使用ACTION_NEW_OUTGOING_CALL意图。

+0

非常感谢您的详细解释和回应。我非常感谢一切。是的,我读过你建议的那个,但我会再次。我可以用特定的接收器类别获得我的拨出号码。 – 2013-04-11 19:57:51

+0

我正在考虑获取和显示传入呼叫者位置的可能性。我想将发送的号码和位置发送到服务器,在那里它将位置推送到号码被登记的呼叫接收机。你怎么看?做对了吗? – 2013-04-11 19:59:33

+0

我在GPS上遇到以下错误:致命例外:main com.gideon.address.AddressActivity.GetAddress上的java.lang.NullPointerException(AddressActivity.java:109)atcom.gideon.address.AddressActivity $ 3.onClick(AddressActivity。 Java的:80) – 2013-04-11 20:06:29

相关问题