android【打电话,收发短信实现】
1.打电话;
EditText mobileText = (EditText)findViewById(R.id.mobile);
String mobile = mobileText.getText().toString();
Intent intent = new Intent();
intent.setAction("android.intent.action.CALL");
intent.setData(Uri.parse("tel:"+ mobile));
startActivity(intent);
2.发短信
EditText mobileText = (EditText)findViewById(R.id.mobile);
EditText contentText = (EditText)findViewById(R.id.content);
String mobile = mobileText.getText().toString();
String content = contentText.getText().toString();
SmsManager smsManager = SmsManager.getDefault();
ArrayList<String> texts = smsManager.divideMessage(content);//拆分短信
for(String text : texts){
smsManager.sendTextMessage(mobile, null, text, null, null);
}
需在AndroidManifest中声明:<uses-permission android:name="android.permission.SEND_SMS"/>
3.收短信:
public class SMSReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
if ("android.provider.Telephony.SMS_RECEIVED"
.equals(intent.getAction()))
{
String phonenumber = null;
String message=null;
// 接收由SMS传过来的数据
Bundle bundle = intent.getExtras();
// 判断是否有数据
if (bundle != null)
{
// 通过pdus可以获得接收到的所有短信消息
Object[] objArray = (Object[]) bundle.get("pdus");
/* 构建短信对象array,并依据收到的对象长度来创建array的大小 */
SmsMessage[] messages = new SmsMessage[objArray.length];
for (int i = 0; i < objArray.length; i++)
{
messages[i] = SmsMessage
.createFromPdu((byte[]) objArray[i]);
}
for (SmsMessage currentMessage : messages)
{
phonenumber = currentMessage.getDisplayOriginatingAddress();
message = currentMessage.getDisplayMessageBody();
}
}
Toast.makeText(context, "Number="+phonenumber+"Msg="+message, Toast.LENGTH_LONG).show();
}
}
} 需在AndroidManifest中声明:<uses-permission android:name="android.permission.RECEIVE_SMS"/>
posted on 2011-06-11 21:19 android开发实例 阅读(208) 评论(0) 编辑 收藏 举报