我从gpstracking.java调用setmessage函数来设置新意图中的textview值,但是当我运行它时没有显示任何内容。我不知道错误是什么。 我正在发布代码。我正在使用setText来设置值。我正在将纬度和经度作为参数发送给函数。
gpstracking.java
package com.example.gpstracking;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import android.app.Activity;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class AndroidGPSTrackingActivity extends Activity {
Button btnShowLocation;
// GPSTracker class
GPSTracker gps;
List<Address> addresses;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnShowLocation = (Button) findViewById(R.id.btnShowLocation);
final Intent i;
i=new Intent(this,SMS.class);
// show location button click event
btnShowLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// create class object
gps = new GPSTracker(AndroidGPSTrackingActivity.this);
// check if GPS enabled
if(gps.canGetLocation()){
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
// \n is for new line
Geocoder gcd = new Geocoder(AndroidGPSTrackingActivity.this, Locale.getDefault());
try{
List<Address> addresses = gcd.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0)
{
SMS.setmessage(latitude,longitude);
}
}catch(Exception e)
{
System.out.print(e);
}
}else{
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
gps.showSettingsAlert();
}
startActivity(i);
}
});
}
}
SMS.java
package com.example.gpstracking;
import com.example.gpstracking.R;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class SMS extends Activity {
Button btnSendSMS;
EditText phone_number;
static TextView message;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.smsmain);
btnSendSMS = (Button) findViewById(R.id.sendmessage);
phone_number= (EditText) findViewById(R.id.phone_number);
message=(TextView) findViewById(R.id.message);
btnSendSMS.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String phoneNo = phone_number.getText().toString();
String msg = message.getText().toString();
if (phoneNo.length()>0)// && message.length()>0)
sendSMS(phoneNo, msg);
else
Toast.makeText(getBaseContext(),
"Please enter both phone number and message.",
Toast.LENGTH_SHORT).show();
}
});
}
private void sendSMS(String phoneNumber, String message)
{
/*
PendingIntent pi = PendingIntent.getActivity(this, 0,
new Intent(this, test.class), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, pi, null);
*/
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver(){
@SuppressWarnings("deprecation")
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case android.telephony.SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case android.telephony.SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
break;
case android.telephony.SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case android.telephony.SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
//---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
@SuppressWarnings("deprecation")
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
public static void setmessage(double latitude,double longitude)
{
message.setText("my lat long is"+latitude+longitude);
}
}
答案 0 :(得分:0)
试试这个,将纬度和经度放在下一个活动中并获得它。
Intent I = new Intent(AndroidGPSTrackingActivity.this, SMS.class);
I.putExtra("latitude", latitude);
I.putExtra("longitude", longitude);
startActivity(I);
获取下一课的lat和lng。
Intent i = getIntent();
lat = i.getStringExtra("latitude");
lng = i.getStringExtra("longitude");
编辑您的代码:
public class AndroidGPSTrackingActivity extends Activity {
Button btnShowLocation;
// GPSTracker class
GPSTracker gps;
List<Address> addresses;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnShowLocation = (Button) findViewById(R.id.btnShowLocation);
// show location button click event
btnShowLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// create class object
gps = new GPSTracker(AndroidGPSTrackingActivity.this);
// check if GPS enabled
if(gps.canGetLocation()){
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
// \n is for new line
Geocoder gcd = new Geocoder(AndroidGPSTrackingActivity.this, Locale.getDefault());
try{
List<Address> addresses = gcd.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0)
{
SMS.setmessage(latitude,longitude);
}
}catch(Exception e)
{
System.out.print(e);
}
}else{
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
gps.showSettingsAlert();
}
final Intent i;
i=new Intent(this,SMS.class);
i.putExtra("latitude", latitude);
i.putExtra("longitude", longitude);
startActivity(i);
}
});
}
然后在SMS.class onCreate
中获取你的lat和lng。你会得到价值