package com.testing.connection;
import android.app.Activity;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class ConnectionActivity extends Activity implements OnClickListener{
Button press;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
press = (Button)findViewById(R.id.button1);
press.setOnClickListener(this);
}
public void onClick(View view){
ConnectivityManager mgr = (ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE);
boolean is3G = mgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
boolean isWifi = mgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
if(isWifi){
Toast.makeText(this, "WiFi connected...", Toast.LENGTH_LONG).show();
sendMail();
}
else{
//**Turn on Mobile Data
//**Then sendMail()
//**Turn off Mobile Data
}
}
public void sendMail() throws MessagingException{
String host = "smtp.gmail.com";
String password = "abc123";
String from = "testing@gmail.com";
String toAddress = enterEmail.getText().toString();
Properties properties = System.getProperties();
properties.put("mail.smtp.host", host);
properties.put("mail.smtps.auth", true);
properties.put("mail.smtp.starttls.enable", true);
Session session = Session.getInstance(properties, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, toAddress);
message.setSubject("Anti-Theft Attachment");
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("Your email address is saved as backup email in Anti-Theft Application");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
try{
Transport transport = session.getTransport("smtps");
transport.connect(host, from, password);
transport.sendMessage(message, message.getAllRecipients());
System.out.println("Mail Sent Successfully");
transport.close();
} catch (SendFailedException sfe){
System.out.println(sfe);
}
}
}
嗨,我正在开发一个Android应用程序,我想有一个检测到Wifi未连接到手机时自动打开移动数据的功能,因为我想确保无论何时都能发送电子邮件wifi是否连接...所以一旦检测到wifi未连接,则打开3G数据并发送电子邮件并关闭数据网络...
请问如何开启3G网络和关闭3G网络???互联网上的来源稀疏,我希望任何人都可以帮助我解决它...谢谢...