Android编程实现短信收发及语音播报提示功能示例

Android编程实现短信收发及语音播报提示功能示例,第1张

概述本文实例讲述了Android编程实现短信收发及语音播报提示功能。分享给大家供大家参考,具体如下:

本文实例讲述了AndroID编程实现短信收发及语音播报提示功能。分享给大家供大家参考,具体如下:

发送短信功能界面

/** * 发送短信Demo * * @description: * @author ldm * @date 2016-4-22 上午9:07:53 */public class SmsActivity extends Activity implements OnClickListener {  public static final String SMS_RECIPIENT_EXTRA = "com.ldm.study.SMS_RECIPIENT";  public static final String ACTION_SMS_SENT = "com.ldm.study.SMS_SENT_ACTION";  private button sms_send_message;  private EditText sms_recipIEnt;  private EditText sms_content;  private CheckBox sms_enable_receiver;  private TextVIEw sms_status;  @OverrIDe  protected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.send_sms_layout);    initVIEws();    initListeners();    registerSmsReceiver();  }  /**   * 注册短信监听广播   *   * @description:   * @author ldm   * @date 2016-4-22 上午9:23:23   */  private voID registerSmsReceiver() {    registerReceiver(new broadcastReceiver() {      @OverrIDe      public voID onReceive(Context context,Intent intent) {        String message = null;        boolean error = true;        switch (getResultCode()) {        case Activity.RESulT_OK:          message = "发送成功!";          error = false;          break;        case SmsManager.RESulT_ERROR_GENERIC_FAILURE:          message = "发送失败";          break;        case SmsManager.RESulT_ERROR_NO_SERVICE:          message = "发送失败";          break;        case SmsManager.RESulT_ERROR_NulL_PDU:          message = "发送失败";          break;        case SmsManager.RESulT_ERROR_RAdio_OFF:          message = "发送失败";          break;        }        sms_recipIEnt.setEnabled(true);        sms_content.setEnabled(true);        sms_content.setText("");        sms_status.setText(message);        sms_status.setTextcolor(error ? color.RED : color.GREEN);      }    },new IntentFilter(ACTION_SMS_SENT));  }  /**   * 监听事件   *   * @description:   * @author ldm   * @date 2016-4-22 上午9:24:20   */  private voID initListeners() {    sms_send_message.setonClickListener(this);    // 应用Package管理器    final PackageManager pm = this.getPackageManager();    // 获取到应用中指定的组件    final Componentname componentname = new Componentname("com.ldm.study","com.ldm.study.receiver.SmsMessageReceiver");    // 根据组件是否可用的状态来判断sms_enable_receiver的选中状态    sms_enable_receiver        .setChecked(pm.getComponentEnabledSetting(componentname) == PackageManager.COMPONENT_ENABLED_STATE_ENABLED);    // 监听sms_enable_receiver设置广播组件是否可用    sms_enable_receiver        .setonCheckedchangelistener(new OnCheckedchangelistener() {          public voID onCheckedChanged(Compoundbutton buttonVIEw,boolean isChecked) {            // 组件状态设置            pm.setComponentEnabledSetting(                componentname,isChecked ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED                    : PackageManager.COMPONENT_ENABLED_STATE_Disabled,PackageManager.DONT_KILL_APP);          }        });  }  private voID initVIEws() {    sms_recipIEnt = (EditText) (findVIEwByID(R.ID.sms_recipIEnt));    sms_content = ((EditText) findVIEwByID(R.ID.sms_content));    // 是否打开短信广播接收开关    sms_enable_receiver = (CheckBox) findVIEwByID(R.ID.sms_enable_receiver);    sms_send_message = (button) findVIEwByID(R.ID.sms_send_message);    sms_status = (TextVIEw) findVIEwByID(R.ID.sms_status);    if (getIntent().hasExtra(SMS_RECIPIENT_EXTRA)) {      sms_recipIEnt.setText(getIntent().getExtras().getString(          SMS_RECIPIENT_EXTRA));      sms_content.requestFocus();    }  }  @OverrIDe  public voID onClick(VIEw v) {    if (v.getID() == R.ID.sms_send_message) {      sendSms();    }  }  /**   * 发送短信   *   * @description:   * @author ldm   * @date 2016-4-22 上午9:30:38   */  private voID sendSms() {    if (TextUtils.isEmpty(sms_recipIEnt.getText())) {      Toast.makeText(SmsActivity.this,"请输入短信接收者",Toast.LENGTH_SHORT)          .show();      return;    }    if (TextUtils.isEmpty(sms_content.getText())) {      Toast.makeText(SmsActivity.this,"请输入短信内容",Toast.LENGTH_SHORT)          .show();      return;    }    sms_recipIEnt.setEnabled(false);    sms_content.setEnabled(false);    SmsManager sms = SmsManager.getDefault();    List<String> messages = sms.divIDeMessage(sms_recipIEnt.getText()        .toString());    String recipIEnt = sms_content.getText().toString();    for (String message : messages) {      // 发送短信      sms.sendTextMessage(recipIEnt,null,message,PendingIntent          .getbroadcast(SmsActivity.this,new Intent(              ACTION_SMS_SENT),0),null);    }  }}

收到短信后处理界面:

/** * 接收短信语音播报 * * @description: * @author ldm * @date 2016-4-22 上午9:34:21 */public class SmsReceivedActivity extends Activity implements OnInitListener {  private static final int DIALOG_SHOW_MESSAGE = 1;  public static final String SMS_FROM_ADDRESS_EXTRA = "com.ldm.study.SMS_FROM_ADDRESS";  public static final String SMS_FROM_disPLAY_name_EXTRA = "com.ldm.study.SMS_FROM_disPLAY_name";  public static final String SMS_MESSAGE_EXTRA = "com.ldm.study.SMS_MESSAGE";  private TextToSpeech mTts;  private String mFromdisplayname;  private String mFromAddress;  private String mMessage;  private String mFullBodyString;  @SuppressWarnings("deprecation")  @OverrIDe  protected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    // 短信来电号码    mFromAddress = getIntent().getExtras()        .getString(SMS_FROM_ADDRESS_EXTRA);    // 来电人称呼    mFromdisplayname = getIntent().getExtras().getString(        SMS_FROM_disPLAY_name_EXTRA);    mMessage = getIntent().getExtras().getString(SMS_MESSAGE_EXTRA);    // 短信内容    mFullBodyString = String.format(getString(R.string.sms_format),mFromdisplayname,mMessage);    showDialog(DIALOG_SHOW_MESSAGE);    //初始化TTS    mTts = new TextToSpeech(this,this);  }  /**   * 初始化TTS服务   */  @SuppressWarnings("deprecation")  public voID onInit(int status) {    if (status == TextToSpeech.SUCCESS) {// 成功状态      // 设置语言,TTS引擎支持 English、 french 、 German 、 Italian 和 Spanish 这5大语言      int result = mTts.setLanguage(Locale.ENGliSH);      if (result == TextToSpeech.LANG_MISSING_DATA          || result == TextToSpeech.LANG_NOT_SUPPORTED) {        Toast.makeText(this,"TTS不可用",Toast.LENGTH_LONG).show();      } else {        mTts.speak(mFullBodyString,TextToSpeech.QUEUE_ADD,null);        // mTts.speak(mFullBodyString,// "");      }    } else {      Toast.makeText(this,"TTS初始化失败",Toast.LENGTH_LONG).show();    }  }  @OverrIDe  protected Dialog onCreateDialog(int ID) {    switch (ID) {    case DIALOG_SHOW_MESSAGE:      return new AlertDialog.Builder(this)          .setIcon(androID.R.drawable.ic_dialog_email)          .setTitle("Message Received")          .setMessage(mFullBodyString)          .setPositivebutton("回复",new DialogInterface.OnClickListener() {                public voID onClick(DialogInterface dialog,int whichbutton) {                  // 回复短信则跳转到发送短信界面,把接收人信息带过去                  Intent i = new Intent();                  i.setClass(SmsReceivedActivity.this,SmsActivity.class);                  i.putExtra(SmsActivity.SMS_RECIPIENT_EXTRA,mFromAddress);                  startActivity(i);                  dialog.dismiss();                  finish();                }              })          .setNegativebutton("取消",int whichbutton) {                  dialog.dismiss();                  finish();                }              }).create();    }    return null;  }}

短信广播

/** * 短信来信监听广播 * * @description: * @author ldm * @date 2016-4-22 上午10:07:49 */public class MyMsmReceiver extends broadcastReceiver {  @OverrIDe  public voID onReceive(Context context,Intent intent) {    Bundle extras = intent.getExtras();    if (extras == null)      return;    // 收到的短信内容    Object[] pdus = (Object[]) extras.get("pdus");    for (int i = 0; i < pdus.length; i++) {      SmsMessage message = SmsMessage.createFromPdu((byte[]) pdus[i]);      String fromAddress = message.getoriginatingAddress();      String fromdisplayname = fromAddress;      Uri uri;      String[] projection;      uri = Uri.withAppendedpath(          ContactsContract.PhoneLookup.CONTENT_FILTER_URI,Uri.encode(fromAddress));      projection = new String[] { ContactsContract.PhoneLookup.disPLAY_name };      // 查询短信的发件人      Cursor cursor = context.getContentResolver().query(uri,projection,null);      if (cursor != null) {        if (cursor.movetoFirst())          fromdisplayname = cursor.getString(0);        cursor.close();      }      // 跳转到短信接收界面      Intent di = new Intent();      di.setClass(context,SmsReceivedActivity.class);      di.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK          | Intent.FLAG_ACTIVITY_SINGLE_top);      di.putExtra(SmsReceivedActivity.SMS_FROM_ADDRESS_EXTRA,fromAddress);      di.putExtra(SmsReceivedActivity.SMS_FROM_disPLAY_name_EXTRA,fromdisplayname);      di.putExtra(SmsReceivedActivity.SMS_MESSAGE_EXTRA,message          .getMessageBody().toString());      context.startActivity(di);    }  }}

附:DEMO示例代码点击此处本站下载

更多关于AndroID相关内容感兴趣的读者可查看本站专题:《Android短信与电话 *** 作技巧汇总》、《Android文件 *** 作技巧汇总》、《Android *** 作json格式数据技巧总结》、《Android编程之activity *** 作技巧总结》、《Android开发入门与进阶教程》、《Android资源 *** 作技巧汇总》、《Android视图View技巧总结》及《Android控件用法总结》

希望本文所述对大家AndroID程序设计有所帮助。

总结

以上是内存溢出为你收集整理的Android编程实现短信收发及语音播报提示功能示例全部内容,希望文章能够帮你解决Android编程实现短信收发及语音播报提示功能示例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1144771.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存