私はアンドロイドで新しいです。コードを書きました。そして私の目的は、SMSを受け取ったときに自分の現在地を着信SMS番号に送信することです。
SMSを取得して送信できますが、現在地(緯度と経度)を送信できません。どうやってやるの。私を信じてください、私はそれをどのように行うのか分かりません。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
}
}