标签:android style blog color os io
package com.example.message; import android.app.Activity; import android.os.Bundle; import android.telephony.SmsManager; import android.text.TextUtils; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener { private EditText et_number; private EditText et_message; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button send=(Button) findViewById(R.id.send); et_number= (EditText) MainActivity.this.findViewById(R.id.et_number); et_message=(EditText) MainActivity.this.findViewById(R.id.et_message); send.setOnClickListener(this); } @Override public void onClick(View v) { switch(v.getId()){ case R.id.send: sendmessage(); break; } } private void sendmessage() { String number=et_number.getText().toString().trim(); String message=et_message.getText().toString().trim(); if(TextUtils.isEmpty(number)||TextUtils.isEmpty(message)){ Toast.makeText(this, "电话号码或短信内容不能为空",Toast.LENGTH_SHORT).show(); return; }else{ SmsManager smsManager =SmsManager.getDefault(); smsManager.sendTextMessage(number, null, message, null, null); } } }
学了第一课,第二课仿照第一课变得很容易,唯一难点是
SmsManager smsManager =SmsManager.getDefault();
smsManager.sendTextMessage(number, null, message, null, null);
这两行代码实现的就是短信的发送。
还有需要注意的是第一课中需要添加权限,就是打电话的权限就是在AndroidManifest.xml中找到permissions,找到CALL_PHONE
同理发短信的权限是SEND_SMS
标签:android style blog color os io
原文地址:http://www.cnblogs.com/Yvettey-me/p/3842449.html