无标题文档
人多不足以依赖,要生存只有靠自己。
      深窥自己的心,而后发觉一切的奇迹在你自己。
          凡事皆有终结,因此,耐心是赢得成功的一种手段。

Android开发之调用(Call, Dial, SMSManager, Broadcast, Email)

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

<ignore_js_op>1.jpg

先编写主界面Activaty,创建类CallAndSms作为为默认启动页

  1. package jtapp.callandsms;
  2. import java.util.List;
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.net.Uri;
  6. import android.os.Bundle;
  7. import android.telephony.SmsManager;
  8. import android.view.View;
  9. import android.view.View.OnClickListener;
  10. import android.widget.Button;
  11. import android.widget.Toast;
  12. public class CallAndSms extends Activity {
  13.     /** Called when the activity is first created. */
  14.     @Override
  15.     public void onCreate(Bundle savedInstanceState) {
  16.         super.onCreate(savedInstanceState);
  17.         setContentView(R.layout.main);
  18.        
  19.         setComponent();
  20.     }
  21.     
  22.         private void setComponent() {
  23.                 Button bt1 = (Button) findViewById(R.id.Button01);
  24.                 bt1.setOnClickListener(new OnClickListener() {
  25.                         @Override
  26.                         public void onClick(View v) {
  27.                                 Intent intent = new Intent(
  28.                                                 Intent.ACTION_CALL, Uri.parse("tel:10010"));
  29.                                 startActivity(intent);
  30.                         }
  31.                 });
  32.                 Button bt2 = (Button) findViewById(R.id.Button02);
  33.                 bt2.setOnClickListener(new OnClickListener() {
  34.                         @Override
  35.                         public void onClick(View v) {
  36.                                 String smsContent = "102";
  37.                                 // note: SMS must be divided before being sent  
  38.                                 SmsManager sms = SmsManager.getDefault();
  39.                                 List<String> texts = sms.divideMessage(smsContent);
  40.                                 for (String text : texts) {
  41.                                         sms.sendTextMessage("10010", null, text, null, null);
  42.                                 }
  43.                                 // note: not checked success or failure yet
  44.                                 Toast.makeText(
  45.                                                 CallAndSms.this, 
  46.                                                 "短信已发送",
  47.                                                 Toast.LENGTH_SHORT ).show();
  48.                         }
  49.                 });
  50.                 
  51.                 Button bt3 = (Button) findViewById(R.id.Button03);
  52.                 bt3.setOnClickListener(new OnClickListener() {
  53.                         @Override
  54.                         public void onClick(View v) {
  55.                                 Intent intent = new Intent(
  56.                                                 Intent.ACTION_DIAL, Uri.parse("tel:10010"));
  57.                                 startActivity(intent);
  58.                         }
  59.                 });
  60.                 Button bt4 = (Button) findViewById(R.id.Button04);
  61.                 bt4.setOnClickListener(new OnClickListener() {
  62.                         @Override
  63.                         public void onClick(View v) {
  64.                                 Uri uri = Uri.parse("smsto:10010");        
  65.                                 Intent it = new Intent(Intent.ACTION_SENDTO, uri);        
  66.                                 it.putExtra("sms_body", "102");        
  67.                                 startActivity(it); 
  68.                         }
  69.                 });
  70.         }
  71. }


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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.         android:orientation="vertical" android:layout_width="fill_parent"
  4.         android:layout_height="fill_parent" android:gravity="center">
  5.         <TextView android:layout_width="fill_parent"
  6.                 android:layout_height="wrap_content" android:text="Direct Method:"
  7.                 android:gravity="center" />
  8.         <Button android:text="拨打10010客服电话" android:id="@+id/Button01"
  9.                 android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
  10.         <Button android:text="短信10010查余额" android:id="@+id/Button02"
  11.                 android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
  12.         <TextView android:text="InDirect Method:" android:id="@+id/TextView01"
  13.                 android:layout_width="wrap_content" android:layout_height="wrap_content" />
  14.         <Button android:text="拨打10010客服电话" android:id="@+id/Button03"
  15.                 android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
  16.         <Button android:text="短信10010查余额" android:id="@+id/Button04"
  17.                 android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
  18. </LinearLayout>

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

ReceiverSMS.java 代码:
  1. package jtapp.callandsms;
  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.telephony.SmsMessage;
  7. import android.widget.Toast;
  8. public class ReceiverSMS extends BroadcastReceiver {
  9.         @Override
  10.         public void onReceive(Context context, Intent intent) {
  11.                 if (intent.getAction().equals(
  12.                                 "android.provider.Telephony.SMS_RECEIVED")) {
  13.                         StringBuilder sb = new StringBuilder();
  14.                         Bundle bundle = intent.getExtras();
  15.                         if (bundle != null) {
  16.                                 Object[] pdus = (Object[]) bundle.get("pdus");
  17.                                 SmsMessage[] msgs = new SmsMessage[pdus.length];
  18.                                 for (int i = 0; i < pdus.length; i++) {
  19.                                         msgs[i] = SmsMessage
  20.                                                 .createFromPdu((byte[]) pdus[i]);
  21.                                 }
  22.                                 for (SmsMessage s : msgs) {
  23.                                         sb.append("收到来自");
  24.                                         sb.append(s.getDisplayOriginatingAddress());
  25.                                         sb.append("的SMS, 内容:");
  26.                                         sb.append(s.getDisplayMessageBody());
  27.                                 }
  28.                                 Toast.makeText(
  29.                                                 context, 
  30.                                                 "收到了短消息: " + sb.toString(),
  31.                                                 Toast.LENGTH_LONG).show();
  32.                         }
  33.                 
  34.                 }
  35.         }
  36. }

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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.         package="jtapp.callandsms" android:versionCode="1" android:versionName="1.0">
  4.         <application android:icon="@drawable/icon" android:label="@string/app_name"
  5.                 android:debuggable="true">
  6.                 <activity android:name=".CallAndSms" android:label="@string/app_name">
  7.                         <intent-filter>
  8.                                 <action android:name="android.intent.action.MAIN" />
  9.                                 <category android:name="android.intent.category.LAUNCHER" />
  10.                         </intent-filter>
  11.                 </activity>
  12.                 <receiver android:name=".ReceiverSMS" android:enabled="true">
  13.                         <intent-filter>
  14.                                 <action android:name="android.provider.Telephony.SMS_RECEIVED" />
  15.                         </intent-filter>
  16.                 </receiver>
  17.         </application>
  18.         <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
  19.         <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
  20.         <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
  21. </manifest>

补充,发Email 代码片段:

  1.   //Email
  2.                 Button bt5 = (Button) findViewById(R.id.Button05);
  3.                 bt5.setOnClickListener(new OnClickListener() {
  4.                         @Override
  5.                         public void onClick(View v) {
  6.                                 // Setup the recipient in a String array
  7.                                 String[] mailto = { "noam@gmail.com" };
  8.                                 // Create a new Intent to send messages
  9.                                 Intent sendIntent = new Intent(Intent.ACTION_SEND);
  10.                                 // Write the body of theEmail
  11.                                 String emailBody = "You're password is: ";
  12.                                 // Add attributes to the intent
  13.                                 //sendIntent.setType("text/plain"); // use this line for testing
  14.                                                                                                         // in the emulator
  15.                                 sendIntent.setType("message/rfc822"); // use this line for testing 
  16.                                                                                                                 // on the real phone
  17.                                 sendIntent.putExtra(Intent.EXTRA_EMAIL, mailto);
  18.                                 sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Your Password");
  19.                                 sendIntent.putExtra(Intent.EXTRA_TEXT, emailBody);
  20.                                 startActivity(sendIntent);
  21.                         }
  22.                 });

 

posted @ 2013-03-22 16:20  酷玩时刻  阅读(1291)  评论(0编辑  收藏  举报
友情链接:快递查询 快递查询