我想创建一个应用程序,如果我以用户身份连接互联网并使用电子邮件意图,则通过电子邮件向公司发送一些信息
String priceMassage = creatOrderSummery(price, hasWippedCream, hasChocolate, name);
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto: ")); // only email apps should handle this
intent.putExtra(Intent.EXTRA_SUBJECT, "Just Java order for " + name);
intent.putExtra(Intent.EXTRA_TEXT, priceMassage);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
displayMessage(priceMassage);
但如果我不上网,我希望通过短信发送到公司手机号码的信息,我该怎么办?
答案 0 :(得分:0)
您需要检查设备是否已连接到互联网
您可以通过ConnectionManager
课程查看互联网连接。
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
cm.getActiveNetworkInfo().isConnected(); // return true if internet or otherwise flase
if (internet){
// Email send code here
} else {
// SMS send code here
}
这是样本解决方案
答案 1 :(得分:0)
为了发送短信
在AndroidManifest.xml中添加权限
<uses-permission android:name="android.permission.SEND_SMS" />
public void sendSMS(String phoneNo, String msg) {
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, msg, null, null);
Toast.makeText(getApplicationContext(), "Message Sent",
Toast.LENGTH_LONG).show();
} catch (Exception ex) {
Toast.makeText(getApplicationContext(),ex.getMessage().toString(),
Toast.LENGTH_LONG).show();
ex.printStackTrace();
}
}
答案 2 :(得分:-1)
使用此代码检查主要
中的intenet连接TestInternet testInternet = new TestInternet();
testInternet.execute();
out of main
class TestInternet extends AsyncTask<Void, Void, Boolean>
{
@Override
protected Boolean doInBackground(Void... params) {
try {
URL url = new URL("http://www.google.com");
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setConnectTimeout(4000);
urlc.connect();
if (urlc.getResponseCode() == 200) {
return true;
}
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return false;
}
@Override
protected void onPostExecute(Boolean result)
{
if (!result)
{
// send sms
else
{
//send mail
}
}
}