如何制作网络类型首选项?

时间:2012-10-14 04:12:38

标签: android

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)...希望有人可以帮助我,谢谢......

1 个答案:

答案 0 :(得分:1)

您可以注册一个BroadcastReceiver,以便在建立或已更改WiFi连接时收到通知。

注册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
    }