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是否存在)...希望有人能幫助我,謝謝...如何製作網絡類型偏好?
如果你只是需要確定無線網絡是否已連接這個問題可能會有所幫助:http://stackoverflow.com/questions/5888502/android-wifi-how-to -detect-當-WiFi的連接已經-被確立 –