我是安卓新手。我写了一个代码。我的目的是当我收到短信时,将我的位置发送到传入的短信号码。
我可以接收和发送短信,但我不能发送我的位置(如纬度和经度)。我怎样才能做到这一点。相信我,我不知道我是怎么做到的。我在 onlocationChange() 方法中写了一些代码。但它没有给我位置。
请给我一个建议。
谢谢你的帮助。
这是我的代码:
package com.example.smsmessaging;
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.telephony.TelephonyManager;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
@SuppressLint("UnlocalizedSms")
public class SmsReceiver extends BroadcastReceiver implements LocationListener
{
@SuppressLint({ "NewApi", "NewApi", "NewApi" })
@Override
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
SmsMessage[] mesaj = null;
String str = "";
String sender = "";
if (bundle != null)
{
Object[] obj = (Object[]) bundle.get("pdus");
mesaj = new SmsMessage[obj.length];
for (int i=0; i<mesaj.length; i++){
mesaj[i] = SmsMessage.createFromPdu((byte[])obj[i]);
sender = mesaj[i].getOriginatingAddress();
str += mesaj[i].getMessageBody().toString();
}
Toast.makeText(context,"SMS," + sender + " tarafından gönderildi\n\nSMS in içeriği : " +
str, Toast.LENGTH_LONG).show();
TelephonyManager tMgr =(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
String mIMEINumber = tMgr.getDeviceId();
Toast.makeText(context,"My Phone no : " + mPhoneNumber, Toast.LENGTH_LONG).show();
Toast.makeText(context,"My IMEI no : " + mIMEINumber, Toast.LENGTH_LONG).show();
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(sender.toString(), null, str, null, null);
Toast.makeText(context,"SMS sent ...", Toast.LENGTH_LONG).show();
}
}
public void onLocationChanged(Location location) {
int lat = Integer.parseInt(location.getLatitude()+" ");
int lon = Integer.parseInt(location.getLongitude()+" ");
String efe = "Location " + " Longitude : " + lon + "\n Latitude : " + lat;
Context context = null;
try {
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage("1555215554", null, efe, null, null);
Toast.makeText(context,"location sent. Details : " + efe, Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}