我想从主菜单反向地理编码方法活动传递字符串构建器值以自动发送sms活动
这是我的代码: menu.java
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import mo.locationtracking.GPSTracker;
import mo.sms.IncomingSms;
import info.MonitoringObjek.R;
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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class Menu extends Activity implements OnClickListener{
private Button bTambah;
private Button bLihat;
//private Button blokasi;
GPSTracker gps;
private TextView myAddress;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
myAddress = (TextView)findViewById(info.MonitoringObjek.R.id.lokasisekarang);
// blokasi=(Button) findViewById(R.id.button_lokasi);
// blokasi.setOnClickListener(this);
bTambah = (Button) findViewById(R.id.button_tambah);
bTambah.setOnClickListener(this);
bLihat = (Button) findViewById(R.id.button_view);
bLihat.setOnClickListener(this);
// create class object
gps = new GPSTracker(Menu.this);
// check if GPS enabled
if(gps.canGetLocation()){
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
getMyLocationAddress(latitude, longitude);
gps.stopUsingGPS();
// \n is for new line
// Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
}else{
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
gps.showSettingsAlert();
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.button_tambah :
Intent i = new Intent(this, CreateData.class);
startActivity(i);
break;
case R.id.button_view :
Intent i2 = new Intent(this, ViewData.class);
startActivity(i2);
break;
}
}
public void getMyLocationAddress(double latitude,double longitude) {
Geocoder geocoder= new Geocoder(this, Locale.ENGLISH);
try {
//Place your latitude and longitude
List<Address> addresses = geocoder.getFromLocation(latitude,longitude, 1);
if(addresses != null) {
Address fetchedAddress = addresses.get(0);
StringBuilder strAddress = new StringBuilder();
for(int i=0; i<fetchedAddress.getMaxAddressLineIndex(); i++) {
strAddress.append(fetchedAddress.getAddressLine(i)).append("\n");
}
Toast.makeText(getApplicationContext(),"I am at: "+strAddress.toString(),Toast.LENGTH_LONG).show();
myAddress.setText("I am at: " +strAddress.toString());
Intent intentsms = new Intent (Menu.this,IncomingSms.class);
intentsms.putExtra("strAddress",strAddress.toString());
startActivity(intentsms);
}
else
Toast.makeText(getApplicationContext(), "No Location found", Toast.LENGTH_LONG).show();
//myAddress.setText("No location found..!");
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(getApplicationContext(),"Could not retrieve address please check your GPS and Network", Toast.LENGTH_LONG).show();
gps.showSettingsAlert();
myAddress.setText("Could not Retrive Address!");
}
}
}
我希望strAdress字符串构建器值传递给此活动
IncomingSMS.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
import mo.database.Menu;
public class IncomingSms extends BroadcastReceiver {
// Get the object of SmsManager
//final SmsManager sms = SmsManager.getDefault();
public final String WHEREAREYOU = "halo";
@Override
public void onReceive(Context context, Intent intent) {
// Retrieves a map of extended data from the intent.
final Bundle bundle = intent.getExtras();
Intent intentsms= getIntent(); // gets the previously created intent
final String strAddress = intentsms.getStringExtra("strAddress");
try {
if (bundle != null) {
Object[] messages = (Object[]) bundle.get("pdus");
SmsMessage[] tempsms = new SmsMessage[messages.length];
for (int i = 0; i < messages.length; i++) {
tempsms[i] = SmsMessage.createFromPdu((byte[]) messages[i]);
String phoneNumber = tempsms[i].getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String message = tempsms[i].getDisplayMessageBody();
Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, "senderNum: "+ senderNum + ", message: " + message, duration);
toast.show();
// sms.sendTextMessage("+00000000", null,"message :"+ message,
//null, null);
} // end for loop
for (SmsMessage msg : tempsms) {
//String phoneNumber = msgLocation.getDisplayOriginatingAddress();
String message = msg.getDisplayMessageBody();
if (message.equals(WHEREAREYOU)){
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage("+628563105567", null,"message :"+ strAddress,
null, null);}
}
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);
}
}
}
但是它会在incomingsms.java中返回一些错误类似于方法getIntent()未定义类型
我不知道出了什么问题
答案 0 :(得分:1)
getIntent();
是Activity
类的方法,而不是BroadcastReceiver
的方法。
如果您想使用从Intent
传递的Activity
,可以使用intent
上收到的onReceive
。
请删除Intent intentsms= getIntent();
并将intentsms
替换为intent