0
public class ReplyWithAddressService extends Service{

public static final String GOOGLE_GEOCODER = "http://maps.googleapis.com/maps/api/geocode/json?latlng=";
private String msgRecipient;
private LocationManager manager;
private MyLocationListener listener;
private static double latitude = -1;
private static double longitude = -1;
private String provider;
private String smsMessageString = "";
public static String filenames = "AntiTheft";
SharedPreferences pref;
String email;

@Override
public IBinder onBind(Intent intent){
    return null;
}

@Override
public void onCreate(){
    super.onCreate();
    Log.d(this.getClass().getName(), "Service created");
    pref = getSharedPreferences(filenames, 0);
    manager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    listener = new MyLocationListener();

    if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
        provider = LocationManager.GPS_PROVIDER;
    }
    else if (manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
        provider = LocationManager.NETWORK_PROVIDER;
    }

    manager.requestLocationUpdates(provider, 0, 0, listener);
}

@Override
public void onStart(Intent intent, int startId){
    super.onStart(intent, startId);
    Log.d(this.getClass().getName(), "Service started");
    //Extract Intent Data
    msgRecipient = intent.getStringExtra("number");
    String emailAddress = pref.getString("keyemail", "");
    String contact1 = pref.getString("contact1", "");
    String contact2 = pref.getString("contact2", "");
    Log.d(this.getClass().getName(), "Number: " + msgRecipient);
    ConnectivityManager cManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cManager.getActiveNetworkInfo();

    //Get Location
    if (ReplyWithAddressService.latitude == -1 || ReplyWithAddressService.longitude == -1){
        Location location = manager.getLastKnownLocation(provider);

        if (location != null){
            ReplyWithAddressService.latitude = location.getLatitude();
            ReplyWithAddressService.longitude = location.getLongitude();

            if (info != null){

                if (info.isConnected()){
                    String address = ReplyWithAddressService.getAddressFromGPSData(ReplyWithAddressService.latitude, ReplyWithAddressService.longitude);
                    smsMessageString += "Current Location: " + address + "."; 
                }
            }

            smsMessageString += "Link: http://maps.google.com/maps?q=" + ReplyWithAddressService.latitude + "+" + ReplyWithAddressService.longitude;
            Log.d("Message", smsMessageString);
        }
        else{
            smsMessageString = "Location Data is Not Available";
        }
    }

    SmsManager sManager = SmsManager.getDefault();
    String number = msgRecipient;
    String contactNo1 = contact1;
    String contactNo2 = contact2;
    email = emailAddress; 

    //Send SMS message
    sManager.sendTextMessage(number, null, smsMessageString, null, null);
    sManager.sendTextMessage(contactNo1, null, smsMessageString, null, null);
    sManager.sendTextMessage(contactNo2, null, smsMessageString, null, null);

    try{
        sendMail();
    } catch(MessagingException e){
        e.printStackTrace();
    }

    stopSelf(startId);
}

私はロケーショントラッカーモバイルアプリを開発しています。紛失した携帯電話がロケーションを取得すると、現在のロケーションが事前定義されたメールアドレスと携帯電話番号に送信されます。電話がWi-Fiに接続されていれば問題ありませんが、Wi-Fiをオフにすると、メールで位置情報を受信できなくなります。確認方法を知りたいだけです。モバイルに接続されているWi-Fiの場合は、Wi-Fiが優先されます。そうでない場合は、メールメッセージを送信するためにモバイルネットワークを自動的に有効にします。私の目的は、(Wi-Fiが存在するかどうかに関係なく)どのような条件下でもメールを送信できるようにすることです...誰かが私を助けてくれることを願っています、ありがとう...

4

1 に答える 1

1

WiFi 接続が確立されたとき、または変更されたときに通知されるように、BroadcastReceiver を登録できます。

BroadcastReceiver を登録します。

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
registerReceiver(broadcastReceiver, intentFilter);

そして、その BroadcastReceiver では、次のようなものを使用できます。

  @Override
public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();
    if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) {
        if (intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false)) {
            //wifi is connected. You can do stuff here.
        } else {
            // wifi connection is gone.
        }
    }

BroadcastReceiver のドキュメント: http://developer.android.com/reference/android/content/BroadcastReceiver.html

WifiManager のドキュメント: http://developer.android.com/reference/android/net/wifi/WifiManager.html

上記のコードは接続状態が変更されたときにのみ通知するため、上記のコードを使用する前に、デバイスが既に wifi に接続されているかどうかを確認する必要があります。そもそもつながっている。それを確認するには、次を使用できます。

WifiManager wifiManager = Context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    if (wifiInfo != null) {
        //connection is established
    }
于 2012-10-14T06:35:50.133 に答える