本文來自 http://blog.csdn.net/xjanker2,引用轉載必須註明出處!
打電話和發簡訊可以說是最核心的應用了,本文就來闡述它的調用方法。可以分為直接調用--直接電話或簡訊發出,已經間接調用--進入撥號或簡訊撰寫頁面,等待使用者確認內容後由使用者發出.
先看代碼效果:
先編寫主介面Activaty,建立類CallAndSms作為為預設啟動頁 package jtapp.callandsms;</p><p>import java.util.List;</p><p>import android.app.Activity;<br />import android.content.Intent;<br />import android.net.Uri;<br />import android.os.Bundle;<br />import android.telephony.SmsManager;<br />import android.view.View;<br />import android.view.View.OnClickListener;<br />import android.widget.Button;<br />import android.widget.Toast;</p><p>public class CallAndSms extends Activity {<br /> /** Called when the activity is first created. */<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);</p><p> setComponent();<br /> }</p><p>private void setComponent() {<br />Button bt1 = (Button) findViewById(R.id.Button01);<br />bt1.setOnClickListener(new OnClickListener() {<br />@Override<br />public void onClick(View v) {<br />Intent intent = new Intent(<br />Intent.ACTION_CALL, Uri.parse("tel:10010"));<br />startActivity(intent);<br />}<br />});<br />Button bt2 = (Button) findViewById(R.id.Button02);<br />bt2.setOnClickListener(new OnClickListener() {<br />@Override<br />public void onClick(View v) {<br />String smsContent = "102";<br />// note: SMS must be divided before being sent<br />SmsManager sms = SmsManager.getDefault();<br />List<String> texts = sms.divideMessage(smsContent);<br />for (String text : texts) {<br />sms.sendTextMessage("10010", null, text, null, null);<br />}<br />// note: not checked success or failure yet<br />Toast.makeText(<br />CallAndSms.this,<br />"簡訊已發送",<br />Toast.LENGTH_SHORT ).show();<br />}<br />});</p><p>Button bt3 = (Button) findViewById(R.id.Button03);<br />bt3.setOnClickListener(new OnClickListener() {<br />@Override<br />public void onClick(View v) {<br />Intent intent = new Intent(<br />Intent.ACTION_DIAL, Uri.parse("tel:10010"));<br />startActivity(intent);<br />}<br />});<br />Button bt4 = (Button) findViewById(R.id.Button04);<br />bt4.setOnClickListener(new OnClickListener() {<br />@Override<br />public void onClick(View v) {<br />Uri uri = Uri.parse("smsto:10010");<br />Intent it = new Intent(Intent.ACTION_SENDTO, uri);<br />it.putExtra("sms_body", "102");<br />startActivity(it);<br />}<br />});<br />}<br />}
主介面ui定義 main.xml 代碼:
<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br />android:orientation="vertical" android:layout_width="fill_parent"<br />android:layout_height="fill_parent" android:gravity="center"><br /><TextView android:layout_width="fill_parent"<br />android:layout_height="wrap_content" android:text="Direct Method:"<br />android:gravity="center" /><br /><Button android:text="撥打10010客服電話" android:id="@+id/Button01"<br />android:layout_width="wrap_content" android:layout_height="wrap_content"></Button><br /><Button android:text="簡訊10010查餘額" android:id="@+id/Button02"<br />android:layout_width="wrap_content" android:layout_height="wrap_content"></Button><br /><TextView android:text="InDirect Method:" android:id="@+id/TextView01"<br />android:layout_width="wrap_content" android:layout_height="wrap_content" /><br /><Button android:text="撥打10010客服電話" android:id="@+id/Button03"<br />android:layout_width="wrap_content" android:layout_height="wrap_content"></Button><br /><Button android:text="簡訊10010查餘額" android:id="@+id/Button04"<br />android:layout_width="wrap_content" android:layout_height="wrap_content"></Button><br /></LinearLayout>
用於監聽簡訊到來的Broadcast訊息的檔案,
ReceiverSMS.java 代碼:
package jtapp.callandsms;</p><p>import android.content.BroadcastReceiver;<br />import android.content.Context;<br />import android.content.Intent;<br />import android.os.Bundle;<br />import android.telephony.SmsMessage;<br />import android.widget.Toast;</p><p>public class ReceiverSMS extends BroadcastReceiver {</p><p>@Override<br />public void onReceive(Context context, Intent intent) {<br />if (intent.getAction().equals(<br />"android.provider.Telephony.SMS_RECEIVED")) {<br />StringBuilder sb = new StringBuilder();<br />Bundle bundle = intent.getExtras();<br />if (bundle != null) {<br />Object[] pdus = (Object[]) bundle.get("pdus");<br />SmsMessage[] msgs = new SmsMessage[pdus.length];<br />for (int i = 0; i < pdus.length; i++) {<br />msgs[i] = SmsMessage<br />.createFromPdu((byte[]) pdus[i]);<br />}<br />for (SmsMessage s : msgs) {<br />sb.append("收到來自");<br />sb.append(s.getDisplayOriginatingAddress());<br />sb.append("的SMS, 內容:");<br />sb.append(s.getDisplayMessageBody());<br />}<br />Toast.makeText(<br />context,<br />"收到了短訊息: " + sb.toString(),<br />Toast.LENGTH_LONG).show();<br />}</p><p>}<br />}<br />}<br />
AndroidManifest.xml中許可權、activity和receiver的設定:
<?xml version="1.0" encoding="utf-8"?><br /><manifest xmlns:android="http://schemas.android.com/apk/res/android"<br />package="jtapp.callandsms" android:versionCode="1" android:versionName="1.0"><br /><application android:icon="@drawable/icon" android:label="@string/app_name"<br />android:debuggable="true"><br /><activity android:name=".CallAndSms" android:label="@string/app_name"><br /><intent-filter><br /><action android:name="android.intent.action.MAIN" /><br /><category android:name="android.intent.category.LAUNCHER" /><br /></intent-filter><br /></activity><br /><receiver android:name=".ReceiverSMS" android:enabled="true"><br /><intent-filter><br /><action android:name="android.provider.Telephony.SMS_RECEIVED" /><br /></intent-filter><br /></receiver><br /></application><br /><uses-permission android:name="android.permission.CALL_PHONE"></uses-permission><br /><uses-permission android:name="android.permission.SEND_SMS"></uses-permission><br /><uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission><br /></manifest>
補充,發Email 程式碼片段:
//Email<br />Button bt5 = (Button) findViewById(R.id.Button05);<br />bt5.setOnClickListener(new OnClickListener() {<br />@Override<br />public void onClick(View v) {<br />// Setup the recipient in a String array<br />String[] mailto = { "noam@gmail.com" };<br />// Create a new Intent to send messages<br />Intent sendIntent = new Intent(Intent.ACTION_SEND);<br />// Write the body of theEmail<br />String emailBody = "You're password is: ";<br />// Add attributes to the intent<br />//sendIntent.setType("text/plain"); // use this line for testing<br />// in the emulator<br />sendIntent.setType("message/rfc822"); // use this line for testing<br />// on the real phone<br />sendIntent.putExtra(Intent.EXTRA_EMAIL, mailto);<br />sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Your Password");<br />sendIntent.putExtra(Intent.EXTRA_TEXT, emailBody);<br />startActivity(sendIntent);<br />}<br />});
擴充閱讀:
Android下調用收發簡訊郵件等
http://sean.huanglijiang.com/article.asp?id=218
android 簡訊發送全過程
http://apps.hi.baidu.com/share/detail/15096826