EX06_14.java
上传用户:vip_99
上传日期:2021-03-27
资源大小:61159k
文件大小:5k
源码类别:

android开发

开发平台:

Java

  1. package irdc.ex06_14;
  2. import android.app.Activity;
  3. import android.app.PendingIntent;
  4. import android.content.BroadcastReceiver;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.content.IntentFilter;
  8. import android.os.Bundle;
  9. import android.telephony.gsm.SmsManager;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.EditText;
  13. import android.widget.TextView;
  14. import android.widget.Toast;
  15. public class EX06_14 extends Activity
  16. {
  17.   /* 创建两个mServiceReceiver对象,作为类成员变量 */
  18.   private mServiceReceiver mReceiver01, mReceiver02;
  19.   private Button mButton1;
  20.   private TextView mTextView01;
  21.   private EditText mEditText1, mEditText2;
  22.   
  23.   /* 自定义ACTION常数,作为广播的Intent Filter识别常数 */
  24.   private String SMS_SEND_ACTIOIN = "SMS_SEND_ACTIOIN";
  25.   private String SMS_DELIVERED_ACTION = "SMS_DELIVERED_ACTION";
  26.     
  27.   /** Called when the activity is first created. */
  28.   @Override
  29.   public void onCreate(Bundle savedInstanceState)
  30.   {
  31.     super.onCreate(savedInstanceState);
  32.     setContentView(R.layout.main);
  33.     
  34.     mTextView01 = (TextView)findViewById(R.id.myTextView1);
  35.     
  36.     /* 电话号码 */
  37.     mEditText1 = (EditText) findViewById(R.id.myEditText1);
  38.     
  39.     /* 短信内容 */
  40.     mEditText2 = (EditText) findViewById(R.id.myEditText2);
  41.     mButton1 = (Button) findViewById(R.id.myButton1);
  42.     
  43.     //mEditText1.setText("+886935123456");
  44.     /* 设置默认为5556表示第二个模拟器的Port */
  45.     mEditText1.setText("5556");
  46.     mEditText2.setText("Hello DavidLanz!");
  47.     
  48.     /* 发送SMS短信按钮事件处理 */
  49.     mButton1.setOnClickListener(new Button.OnClickListener()
  50.     {
  51.       @Override
  52.       public void onClick(View arg0)
  53.       {
  54.         // TODO Auto-generated method stub
  55.         
  56.         /* 欲发送的电话号码 */
  57.         String strDestAddress = mEditText1.getText().toString();
  58.         
  59.         /* 欲发送的短信内容 */
  60.         String strMessage = mEditText2.getText().toString();
  61.         
  62.         /* 创建SmsManager对象 */
  63.         SmsManager smsManager = SmsManager.getDefault();
  64.         
  65.         // TODO Auto-generated method stub
  66.         try
  67.         {
  68.           /* 创建自定义Action常数的Intent(给PendingIntent参数之用) */
  69.           Intent itSend = new Intent(SMS_SEND_ACTIOIN);
  70.           Intent itDeliver = new Intent(SMS_DELIVERED_ACTION);
  71.           
  72.           /* sentIntent参数为传送后接受的广播信息PendingIntent */
  73.           PendingIntent mSendPI = PendingIntent.getBroadcast
  74.           (getApplicationContext(), 0, itSend, 0);
  75.           
  76.           /* deliveryIntent参数为送达后接受的广播信息PendingIntent */
  77.           PendingIntent mDeliverPI = PendingIntent.getBroadcast
  78.           (getApplicationContext(), 0, itDeliver, 0);
  79.           
  80.           /* 发送SMS短信,注意倒数的两个PendingIntent参数 */
  81.           smsManager.sendTextMessage
  82.           (strDestAddress, null, strMessage, mSendPI, mDeliverPI);
  83.           
  84.           mTextView01.setText(R.string.str_sms_sending);
  85.         }
  86.         catch(Exception e)
  87.         {
  88.           mTextView01.setText(e.toString());
  89.           e.printStackTrace();
  90.         }
  91.       }
  92.     });
  93.   }
  94.   
  95.   /* 自定义mServiceReceiver覆盖BroadcastReceiver聆听短信状态信息 */
  96.   public class mServiceReceiver extends BroadcastReceiver
  97.   {
  98.     @Override
  99.     public void onReceive(Context context, Intent intent)
  100.     {
  101.       // TODO Auto-generated method stub
  102.       
  103.       try
  104.       {
  105.         /* android.content.BroadcastReceiver.getResultCode()方法 */
  106.         switch(getResultCode())
  107.         {
  108.           case Activity.RESULT_OK:
  109.             /* 发送短信成功 */
  110.             //mTextView01.setText(R.string.str_sms_sent_success);
  111.             mMakeTextToast
  112.             (
  113.               getResources().getText
  114.               (R.string.str_sms_sent_success).toString(),
  115.               true
  116.             );
  117.             break;
  118.           case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
  119.             /* 发送短信失败 */
  120.             //mTextView01.setText(R.string.str_sms_sent_failed);
  121.             mMakeTextToast
  122.             (
  123.               getResources().getText
  124.               (R.string.str_sms_sent_failed).toString(),
  125.               true
  126.             );
  127.             break;
  128.           case SmsManager.RESULT_ERROR_RADIO_OFF:
  129.             break;
  130.           case SmsManager.RESULT_ERROR_NULL_PDU:
  131.             break;
  132.         }        
  133.       }
  134.       catch(Exception e)
  135.       {
  136.         mTextView01.setText(e.toString());
  137.         e.getStackTrace();
  138.       }
  139.     }
  140.   }
  141.   
  142.   public void mMakeTextToast(String str, boolean isLong)
  143.   {
  144.     if(isLong==true)
  145.     {
  146.       Toast.makeText(EX06_14.this, str, Toast.LENGTH_LONG).show();
  147.     }
  148.     else
  149.     {
  150.       Toast.makeText(EX06_14.this, str, Toast.LENGTH_SHORT).show();
  151.     }
  152.   }
  153.   
  154.   @Override
  155.   protected void onResume()
  156.   {
  157.     // TODO Auto-generated method stub
  158.     
  159.     /* 自定义IntentFilter为SENT_SMS_ACTIOIN Receiver */
  160.     IntentFilter mFilter01;
  161.     mFilter01 = new IntentFilter(SMS_SEND_ACTIOIN);
  162.     mReceiver01 = new mServiceReceiver();
  163.     registerReceiver(mReceiver01, mFilter01);
  164.     
  165.     /* 自定义IntentFilter为DELIVERED_SMS_ACTION Receiver */
  166.     mFilter01 = new IntentFilter(SMS_DELIVERED_ACTION);
  167.     mReceiver02 = new mServiceReceiver();
  168.     registerReceiver(mReceiver02, mFilter01);
  169.     
  170.     super.onResume();
  171.   }
  172.   
  173.   @Override
  174.   protected void onPause()
  175.   {
  176.     // TODO Auto-generated method stub
  177.     
  178.     /* 取消注册自定义Receiver */
  179.     unregisterReceiver(mReceiver01);
  180.     unregisterReceiver(mReceiver02);
  181.     
  182.     super.onPause();
  183.   }
  184. }