我有一些代码可以用来获取经度和纬度...
这是代码
package ncsu.course.android.broadcastchatui1;
import java.util.ArrayList;
import java.util.Locale;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import java.io.IOException;
import java.util.List;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.Address;
import android.location.Geocoder;
import ncsu.course.android.broadcastchatui1.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.Engine;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.speech.RecognizerIntent;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
/**
* This is the main Activity that displays GUI.
*/
public class BroadcastChat extends Activity {
// Debugging
double LATITUDE = 37.42233;
double LONGITUDE = -122.083;
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds
protected LocationManager locationManager;
private static final String TAG = "BcastChatUI";
private static final boolean D = true;
private Context _context;
private TextToSpeech tts = null;
private static int REQ_CODE = 1;
private boolean ttsIsInit = false;
private boolean awalan = false;
private String pesan = "haha";
private String tanya;
private String siapa = "saya";
private int SPEECH_REQUEST_CODE = 1234;
// Layout Views
private ListView mConversationView;
private EditText mOutEditText;
private ImageButton mSendButton;
// Name of the connected device
private ArrayAdapter<String> mConversationArrayAdapter;
private StringBuffer mOutStringBuffer;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
_context = this.getApplicationContext();
if(D) Log.e(TAG, "+++ ON CREATE +++");
/* Use the LocationManager class to obtain GPS locations */
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
new MyLocationListener()
);
// Set up the window layout
setContentView(R.layout.main);
}
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(BroadcastChat.this, message, Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(BroadcastChat.this, "Provider status changed",
Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
Toast.makeText(BroadcastChat.this,
"Provider disabled by the user. GPS turned off",
Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
Toast.makeText(BroadcastChat.this,
"Provider enabled by the user. GPS turned on",
Toast.LENGTH_LONG).show();
}
}
private void cektempat(){
Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);
try {
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
if(addresses != null) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder("Anda ada di ");
for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
}
pesan = strReturnedAddress.toString();
String message = pesan;
sendMessage(message);
startTextToSpeech2();
}
else{
pesan = "Maaf saya tidak tahu anda dimana";
String message = pesan;
sendMessage(message);
startTextToSpeech2();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
pesan = "IO EXCEPCTION ERROR";
String message = pesan;
sendMessage(message);
startTextToSpeech2();
}
}
public void onStart() {
super.onStart();
if(D) Log.e(TAG, "++ ON START ++");
awalan = false;
setupChat();
startTextToSpeech();
String message = "Selamat datang di kiosban";
sendMessage(message);
}
private void startTextToSpeech() {
Intent intent = new Intent(Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(intent, REQ_CODE);
}
private void startTextToSpeech2() {
Intent intent = new Intent(Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(intent, REQ_CODE);
}
private void sendRecognizeIntent()
{
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Apa yang bisa kami bantu?");
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 100);
startActivityForResult(intent, SPEECH_REQUEST_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQ_CODE) {
if (resultCode == Engine.CHECK_VOICE_DATA_PASS) {
tts = new TextToSpeech(this, new OnInitListener() {
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
ttsIsInit = true;
if (tts.isLanguageAvailable(Locale.ENGLISH) >= 0)
tts.setLanguage(Locale.ENGLISH);
tts.setPitch(0.8f);
tts.setSpeechRate(1.0f);
if (awalan == false){
awalan = true;
awalspeak();
}
if (awalan == true){
speak();
}
}
}
});
}
/**if no voice data is currently available,
* start a new activity using the ACTION_INSTALL_TTS_DATA action
* from the TTS Engine class to initiate its installation
*/
else {
Intent installVoice = new Intent(Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installVoice);
}
}
if (requestCode == SPEECH_REQUEST_CODE)
{
if (resultCode == RESULT_OK)
{
ArrayList<String> matches = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if (matches.size() == 0)
{
tts.speak("Heard nothing", TextToSpeech.QUEUE_FLUSH, null);
}
tanya = matches.get(0);
String pertanyaan = tanya;
sendTanya(pertanyaan);
}
}
else
{
Log.d(TAG, "result NOT ok");
}
super.onActivityResult(requestCode, resultCode, data);
}
private void speak() {
if (tts != null && ttsIsInit) {
String text = pesan;
tts.speak(text, TextToSpeech.QUEUE_ADD, null);
}
}
private void jawab() {
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("tanya", tanya));
/* String valid = "1";*/
String response = null;
try {
response = CustomHttpClient.executeHttpPost("http://kiosban.com/android/jamesband.php", postParameters);
String res = response.toString();
res = res.trim();
res = res.replaceAll("\\s+"," ");
pesan = res;
String message = pesan;
if(res.equals("lokasi")){
cektempat();
}else{
sendMessage(message);
startTextToSpeech2();
}
}
catch (Exception e) {}
}
private void awalspeak() {
if (tts != null && ttsIsInit) {
String text = "selamat datang di kiosban";
tts.speak(text, TextToSpeech.QUEUE_ADD, null);
}
}
public synchronized void onResume() {
super.onResume();
if(D) Log.e(TAG, "+ ON RESUME +");
}
private void setupChat() {
Log.d(TAG, "setupChat()");
// Initialize the array adapter for the conversation thread
mConversationArrayAdapter = new ArrayAdapter<String>(this, R.layout.message);
mConversationView = (ListView) findViewById(R.id.in);
mConversationView.setAdapter(mConversationArrayAdapter);
// Initialize the compose field with a listener for the return key
mOutEditText = (EditText) findViewById(R.id.edit_text_out);
mOutEditText.setOnEditorActionListener(mWriteListener);
mOutEditText.setVisibility(View.GONE);
// Initialize the send button with a listener that for click events
mSendButton = (ImageButton) findViewById(R.id.button_send);
mSendButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(D) Log.e(TAG, "[sendButton clicked]");
// Send a message using content of the edit text widget
sendRecognizeIntent();
}
});
// Initialize the buffer for outgoing messages
mOutStringBuffer = new StringBuffer("");
}
public synchronized void onPause() {
super.onPause();
if(D) Log.e(TAG, "- ON PAUSE -");
}
public void onStop() {
super.onStop();
if(D) Log.e(TAG, "-- ON STOP --");
}
public void onDestroy() {
super.onDestroy();
if(D) Log.e(TAG, "--- ON DESTROY ---");
}
private void sendMessage(String message) {
// Check that there's actually something to send
if (message.length() > 0 ) {
// We will send a message via wifi
// Reset out string buffer to zero and clear the edit text field
mOutStringBuffer.setLength(0);
mOutEditText.setText(mOutStringBuffer);
}else{
Toast.makeText(this, "Please type a message", Toast.LENGTH_SHORT).show();
return;
}
// Add message to UI
mConversationArrayAdapter.add("James Band: " + message);
}
private void sendTanya(String pertanyaan) {
// Check that there's actually something to send
if (pertanyaan.length() > 0 ) {
// We will send a message via wifi
// Reset out string buffer to zero and clear the edit text field
mOutStringBuffer.setLength(0);
mOutEditText.setText(mOutStringBuffer);
}else{
Toast.makeText(this, "Please type a message", Toast.LENGTH_SHORT).show();
return;
}
mConversationArrayAdapter.add("Me: " + pertanyaan);
jawab();
}
// The action listener for the EditText widget, to listen for the return key
private TextView.OnEditorActionListener mWriteListener =
new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
// If the action is a key-up event on the return key, send the message
if (actionId == EditorInfo.IME_NULL // return key
&& event.getAction() == KeyEvent.ACTION_UP) // the key has been released
{
String pertanyaan = view.getText().toString();
sendTanya(pertanyaan);
}
if(D) Log.i(TAG, "END onEditorAction");
return true;
}
};
}
当我使用该代码时它的工作,它给出街道名称,脚本给我来自 LATITUDE 值和 LONGITUDE 值的街道名称,我手动插入它,
但是如果我更改下面的代码
List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
在 cektempat() 上;部分到
List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
我的代码不起作用,我被设置了权限,但代码仍然不起作用..
有人可以帮助我吗?谢谢之前
这是日志猫
07-12 15:33:00.099: ERROR/ClockWidget(10768): getOrientation portrait
07-12 15:33:00.099: ERROR/ClockWidget(10768): drawDayText
07-12 15:33:00.099: ERROR/ClockWidget(10768): width= 50
07-12 15:33:00.099: ERROR/ClockWidget(10768): widthText= 62.0
07-12 15:33:00.099: ERROR/ClockWidget(10768): RIGHT
07-12 15:33:08.499: ERROR/AndroidRuntime(13866): /system/csc/customer.xml ==> cannot open file
07-12 15:33:08.499: ERROR/AndroidRuntime(13866): /system/csc/customer.xml can't open file
07-12 15:33:08.499: ERROR/AndroidRuntime(13866): /system/csc/feature.xml ==> cannot open file
07-12 15:33:09.539: ERROR/power(411): *** set_screen_state 1
07-12 15:33:09.549: ERROR/SensorManager(411): registerListener :: handle = 0 name= BMA220 delay= 60000 Listener= android.view.WindowOrientationListener$SensorEventListenerImpl@407b0938
07-12 15:33:09.549: ERROR/SensorHAL(411): +__poll_activate: handle=0 enabled=1
07-12 15:33:09.549: ERROR/SensorHAL(411): > Accelerometer Write /sys/class/input/input3/enable 1
07-12 15:33:09.579: ERROR/SensorManager(411): reg :: handle = 0
07-12 15:33:09.629: ERROR/BcastChatUI(13638): + ON RESUME +
07-12 15:33:10.049: ERROR/ClockWidget(10768): getOrientation portrait
07-12 15:33:10.049: ERROR/ClockWidget(10768): drawDayText
07-12 15:33:10.059: ERROR/ClockWidget(10768): width= 50
07-12 15:33:10.059: ERROR/ClockWidget(10768): widthText= 62.0
07-12 15:33:10.059: ERROR/ClockWidget(10768): RIGHT
07-12 15:33:10.089: ERROR/GlassLockScreenMusicWidget(411): onReceive() : intent=Intent { act=android.intent.action.SCREEN_ON flg=0x40000000 }
07-12 15:33:12.679: ERROR/InputDispatcher(411): channel '40547ff8 ncsu.course.android.broadcastchatui1/ncsu.course.android.broadcastchatui1.BroadcastChat (server)' ~ Consumer closed input channel or an error occurred. events=0x8
07-12 15:33:12.679: ERROR/InputDispatcher(411): channel '40547ff8 ncsu.course.android.broadcastchatui1/ncsu.course.android.broadcastchatui1.BroadcastChat (server)' ~ Channel is unrecoverably broken and will be disposed!
07-12 15:33:13.129: ERROR/Launcher(511): MTP-LAUNCHER: media scanning not yet finished.
07-12 15:33:20.209: ERROR/su(13918): sudb - Opening database
07-12 15:33:20.669: ERROR/su(13918): sudb - Database opened
07-12 15:33:20.699: ERROR/su(13918): sudb - Database closed
07-12 15:33:34.839: ERROR/AndroidRuntime(13967): /system/csc/customer.xml ==> cannot open file
07-12 15:33:34.839: ERROR/AndroidRuntime(13967): /system/csc/customer.xml can't open file
07-12 15:33:34.839: ERROR/AndroidRuntime(13967): /system/csc/feature.xml ==> cannot open file
07-12 15:33:37.419: ERROR/BcastChatUI(13986): +++ ON CREATE +++
07-12 15:33:37.609: ERROR/BcastChatUI(13986): ++ ON START ++
07-12 15:33:37.629: ERROR/BcastChatUI(13986): + ON RESUME +
07-12 15:33:37.649: ERROR/BcastChatUI(13986): - ON PAUSE -
07-12 15:33:37.879: ERROR/BcastChatUI(13986): + ON RESUME +
07-12 15:33:41.229: ERROR/BcastChatUI(13986): - ON PAUSE -
07-12 15:33:42.669: ERROR/UinboxLogger(14022): [UinboxReceiver] onReceive() >> Context is android.app.ReceiverRestrictedContext@40524348
07-12 15:33:44.899: ERROR/BcastChatUI(13986): -- ON STOP --
07-12 15:33:50.679: ERROR/InputDispatcher(411): channel '408bc300 ncsu.course.android.broadcastchatui1/ncsu.course.android.broadcastchatui1.BroadcastChat (server)' ~ Consumer closed input channel or an error occurred. events=0x8
07-12 15:33:50.679: ERROR/InputDispatcher(411): channel '408bc300 ncsu.course.android.broadcastchatui1/ncsu.course.android.broadcastchatui1.BroadcastChat (server)' ~ Channel is unrecoverably broken and will be disposed!
07-12 15:34:00.069: ERROR/ClockWidget(10768): getOrientation portrait
07-12 15:34:00.069: ERROR/ClockWidget(10768): drawDayText
07-12 15:34:00.079: ERROR/ClockWidget(10768): width= 50
07-12 15:34:00.079: ERROR/ClockWidget(10768): widthText= 62.0
07-12 15:34:00.079: ERROR/ClockWidget(10768): RIGHT
07-12 15:34:16.109: ERROR/BcastChatUI(14164): +++ ON CREATE +++
07-12 15:34:16.249: ERROR/BcastChatUI(14164): ++ ON START ++
07-12 15:34:16.299: ERROR/BcastChatUI(14164): + ON RESUME +
07-12 15:34:16.339: ERROR/BcastChatUI(14164): - ON PAUSE -
07-12 15:34:17.129: ERROR/BcastChatUI(14164): + ON RESUME +
07-12 15:34:18.209: ERROR/GPSD(199): find_ril_request: not found.
07-12 15:34:18.399: ERROR/ClockWidget(10768): getOrientation portrait
07-12 15:34:19.579: ERROR/ClockWidget(10768): drawDayText
07-12 15:34:20.079: ERROR/ClockWidget(10768): width= 50
07-12 15:34:20.099: ERROR/ClockWidget(10768): widthText= 62.0
07-12 15:34:20.099: ERROR/ClockWidget(10768): RIGHT
07-12 15:34:26.399: ERROR/AndroidRuntime(14164): FATAL EXCEPTION: main
07-12 15:34:26.399: ERROR/AndroidRuntime(14164): java.lang.NullPointerException
07-12 15:34:26.399: ERROR/AndroidRuntime(14164): at ncsu.course.android.broadcastchatui1.BroadcastChat.cektempat(BroadcastChat.java:147)
07-12 15:34:26.399: ERROR/AndroidRuntime(14164): at ncsu.course.android.broadcastchatui1.BroadcastChat.access$1(BroadcastChat.java:140)
07-12 15:34:26.399: ERROR/AndroidRuntime(14164): at ncsu.course.android.broadcastchatui1.BroadcastChat$1.onEditorAction(BroadcastChat.java:442)
07-12 15:34:26.399: ERROR/AndroidRuntime(14164): at android.widget.TextView.onKeyUp(TextView.java:4721)
07-12 15:34:26.399: ERROR/AndroidRuntime(14164): at android.view.KeyEvent.dispatch(KeyEvent.java:1290)
07-12 15:34:26.399: ERROR/AndroidRuntime(14164): at android.view.View.dispatchKeyEvent(View.java:3855)
07-12 15:34:26.399: ERROR/AndroidRuntime(14164): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)
07-12 15:34:26.399: ERROR/AndroidRuntime(14164): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)
07-12 15:34:26.399: ERROR/AndroidRuntime(14164): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)
07-12 15:34:26.399: ERROR/AndroidRuntime(14164): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)
07-12 15:34:26.399: ERROR/AndroidRuntime(14164): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1746)
07-12 15:34:26.399: ERROR/AndroidRuntime(14164): at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1130)
07-12 15:34:26.399: ERROR/AndroidRuntime(14164): at android.app.Activity.dispatchKeyEvent(Activity.java:2073)
07-12 15:34:26.399: ERROR/AndroidRuntime(14164): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1722)
07-12 15:34:26.399: ERROR/AndroidRuntime(14164): at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2627)
07-12 15:34:26.399: ERROR/AndroidRuntime(14164): at android.view.ViewRoot.handleMessage(ViewRoot.java:2030)
07-12 15:34:26.399: ERROR/AndroidRuntime(14164): at android.os.Handler.dispatchMessage(Handler.java:99)
07-12 15:34:26.399: ERROR/AndroidRuntime(14164): at android.os.Looper.loop(Looper.java:130)
07-12 15:34:26.399: ERROR/AndroidRuntime(14164): at android.app.ActivityThread.main(ActivityThread.java:3687)
07-12 15:34:26.399: ERROR/AndroidRuntime(14164): at java.lang.reflect.Method.invokeNative(Native Method)
07-12 15:34:26.399: ERROR/AndroidRuntime(14164): at java.lang.reflect.Method.invoke(Method.java:507)
07-12 15:34:26.399: ERROR/AndroidRuntime(14164): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
07-12 15:34:26.399: ERROR/AndroidRuntime(14164): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
07-12 15:34:26.399: ERROR/AndroidRuntime(14164): at dalvik.system.NativeStart.main(Native Method)
07-12 15:34:26.409: ERROR/(411): Dumpstate > /data/log/dumpstate_app_error
07-12 15:34:55.339: ERROR/power(411): *** set_screen_state 0
07-12 15:34:55.339: ERROR/SensorManager(411): unregisterListener:: all sensors, listener = com.android.server.PowerManagerService$13@405198e0
07-12 15:34:55.349: ERROR/SensorManager(411): unregisterListener:: all sensors, listener = android.view.WindowOrientationListener$SensorEventListenerImpl@407b0938
07-12 15:34:55.349: ERROR/SensorHAL(411): +__poll_activate: handle=0 enabled=0
07-12 15:34:55.349: ERROR/SensorHAL(411): > Accelerometer Write /sys/class/input/input3/enable 0
07-12 15:35:00.069: ERROR/ClockWidget(10768): getOrientation portrait
07-12 15:35:00.069: ERROR/ClockWidget(10768): drawDayText
07-12 15:35:00.079: ERROR/ClockWidget(10768): width= 50
07-12 15:35:00.079: ERROR/ClockWidget(10768): widthText= 62.0
07-12 15:35:00.079: ERROR/ClockWidget(10768): RIGHT
07-12 15:35:20.939: ERROR/power(411): *** set_screen_state 1
07-12 15:35:20.949: ERROR/SensorManager(411): registerListener :: handle = 0 name= BMA220 delay= 60000 Listener= android.view.WindowOrientationListener$SensorEventListenerImpl@407b0938
07-12 15:35:20.949: ERROR/SensorHAL(411): +__poll_activate: handle=0 enabled=1
07-12 15:35:20.949: ERROR/SensorHAL(411): > Accelerometer Write /sys/class/input/input3/enable 1
07-12 15:35:20.949: ERROR/SensorManager(411): reg :: handle = 0
07-12 15:35:21.209: ERROR/ClockWidget(10768): getOrientation portrait
07-12 15:35:21.209: ERROR/ClockWidget(10768): drawDayText
07-12 15:35:21.209: ERROR/ClockWidget(10768): width= 50
07-12 15:35:21.209: ERROR/ClockWidget(10768): widthText= 62.0
07-12 15:35:21.209: ERROR/ClockWidget(10768): RIGHT
07-12 15:35:21.299: ERROR/GlassLockScreenMusicWidget(411): onReceive() : intent=Intent { act=android.intent.action.SCREEN_ON flg=0x40000000 }