Android應用開發筆記(1):調用打電話和發簡訊、收簡訊介面、發Email (Call, Dial, SMSManager, Broadcast, Email)

來源:互聯網
上載者:User

本文來自 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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.