Android应用开发笔记(1):调用打电话和发短信、收短信接口、发Email (Call, Dial, SMSManager, Broadcast, Email)

本文来自 http://blog.csdn.net/xjanker2,引用转载必须注明出处!

 

打电话和发短信可以说是最核心的应用了,本文就来阐述它的调用方法。可以分为直接调用--直接电话或短信发出,已经间接调用--进入拨号或短信撰写页面,等待用户确认内容后由用户发出.

先看代码效果截图:

先编写主界面Activaty,创建类CallAndSms作为为默认启动页 package jtapp.callandsms; import java.util.List; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.telephony.SmsManager; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class CallAndSms extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setComponent(); } private void setComponent() { Button bt1 = (Button) findViewById(R.id.Button01); bt1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent( Intent.ACTION_CALL, Uri.parse("tel:10010")); startActivity(intent); } }); Button bt2 = (Button) findViewById(R.id.Button02); bt2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String smsContent = "102"; // note: SMS must be divided before being sent SmsManager sms = SmsManager.getDefault(); List texts = sms.divideMessage(smsContent); for (String text : texts) { sms.sendTextMessage("10010", null, text, null, null); } // note: not checked success or failure yet Toast.makeText( CallAndSms.this, "短信已发送", Toast.LENGTH_SHORT ).show(); } }); Button bt3 = (Button) findViewById(R.id.Button03); bt3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent( Intent.ACTION_DIAL, Uri.parse("tel:10010")); startActivity(intent); } }); Button bt4 = (Button) findViewById(R.id.Button04); bt4.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Uri uri = Uri.parse("smsto:10010"); Intent it = new Intent(Intent.ACTION_SENDTO, uri); it.putExtra("sms_body", "102"); startActivity(it); } }); } }

主界面ui定义 main.xml 代码:

用于监听短信到来的Broadcast消息的文件,

ReceiverSMS.java 代码:

package jtapp.callandsms; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.telephony.SmsMessage; import android.widget.Toast; public class ReceiverSMS extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals( "android.provider.Telephony.SMS_RECEIVED")) { StringBuilder sb = new StringBuilder(); Bundle bundle = intent.getExtras(); if (bundle != null) { Object[] pdus = (Object[]) bundle.get("pdus"); SmsMessage[] msgs = new SmsMessage[pdus.length]; for (int i = 0; i < pdus.length; i++) { msgs[i] = SmsMessage .createFromPdu((byte[]) pdus[i]); } for (SmsMessage s : msgs) { sb.append("收到来自"); sb.append(s.getDisplayOriginatingAddress()); sb.append("的SMS, 内容:"); sb.append(s.getDisplayMessageBody()); } Toast.makeText( context, "收到了短消息: " + sb.toString(), Toast.LENGTH_LONG).show(); } } } }

AndroidManifest.xml中权限、activity和receiver的设定:

 

 

补充,发Email 代码片段:

//Email Button bt5 = (Button) findViewById(R.id.Button05); bt5.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // Setup the recipient in a String array String[] mailto = { "[email protected]" }; // Create a new Intent to send messages Intent sendIntent = new Intent(Intent.ACTION_SEND); // Write the body of theEmail String emailBody = "You're password is: "; // Add attributes to the intent //sendIntent.setType("text/plain"); // use this line for testing // in the emulator sendIntent.setType("message/rfc822"); // use this line for testing // on the real phone sendIntent.putExtra(Intent.EXTRA_EMAIL, mailto); sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Your Password"); sendIntent.putExtra(Intent.EXTRA_TEXT, emailBody); startActivity(sendIntent); } });

 

扩展阅读:

Android下调用收发短信邮件等

http://sean.huanglijiang.com/article.asp?id=218

android 短信发送全过程

http://apps.hi.baidu.com/share/detail/15096826

 

你可能感兴趣的:(Android应用,Android开发技术专栏)