您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)Android編程如何實現(xiàn)短信收發(fā)及語音播報提示功能的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
具體如下:
發(fā)送短信功能界面
/** * 發(fā)送短信Demo */ 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(); } /** * 注冊短信監(jiān)聽廣播 */ 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 = "發(fā)送成功!"; error = false; break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: message = "發(fā)送失敗"; break; case SmsManager.RESULT_ERROR_NO_SERVICE: message = "發(fā)送失敗"; break; case SmsManager.RESULT_ERROR_NULL_PDU: message = "發(fā)送失敗"; break; case SmsManager.RESULT_ERROR_RADIO_OFF: message = "發(fā)送失敗"; 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)); } /** * 監(jiān)聽事件 */ private void initListeners() { sms_send_message.setOnClickListener(this); // 應(yīng)用Package管理器 final PackageManager pm = this.getPackageManager(); // 獲取到應(yīng)用中指定的組件 final ComponentName componentName = new ComponentName("com.ldm.study", "com.ldm.study.receiver.SmsMessageReceiver"); // 根據(jù)組件是否可用的狀態(tài)來判斷sms_enable_receiver的選中狀態(tài) sms_enable_receiver .setChecked(pm.getComponentEnabledSetting(componentName) == PackageManager.COMPONENT_ENABLED_STATE_ENABLED); // 監(jiān)聽sms_enable_receiver設(shè)置廣播組件是否可用 sms_enable_receiver .setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // 組件狀態(tài)設(shè)置 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)); // 是否打開短信廣播接收開關(guān) 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(); } } /** * 發(fā)送短信 * */ 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, "請輸入短信內(nèi)容", 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) { // 發(fā)送短信 sms.sendTextMessage(recipient, null, message, PendingIntent .getBroadcast(SmsActivity.this, 0, new Intent( ACTION_SMS_SENT), 0), null); } } }
收到短信后處理界面:
/** * 接收短信語音播報 * */ 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); // 短信內(nèi)容 mFullBodyString = String.format(getString(R.string.sms_format), mFromDisplayName, mMessage); showDialog(DIALOG_SHOW_MESSAGE); //初始化TTS mTts = new TextToSpeech(this, this); } /** * 初始化TTS服務(wù) */ @SuppressWarnings("deprecation") public void onInit(int status) { if (status == TextToSpeech.SUCCESS) {// 成功狀態(tài) // 設(shè)置語言,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, TextToSpeech.QUEUE_ADD, null, // ""); } } 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("回復(fù)", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { // 回復(fù)短信則跳轉(zhuǎn)到發(fā)送短信界面,把接收人信息帶過去 Intent i = new Intent(); i.setClass(SmsReceivedActivity.this, SmsActivity.class); i.putExtra(SmsActivity.SMS_RECIPIENT_EXTRA, mFromAddress); startActivity(i); dialog.dismiss(); finish(); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { dialog.dismiss(); finish(); } }).create(); } return null; } }
短信廣播
/** * 短信來信監(jiān)聽廣播 * */ public class MyMsmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Bundle extras = intent.getExtras(); if (extras == null) return; // 收到的短信內(nèi)容 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 }; // 查詢短信的發(fā)件人 Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null); if (cursor != null) { if (cursor.moveToFirst()) fromDisplayName = cursor.getString(0); cursor.close(); } // 跳轉(zhuǎn)到短信接收界面 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); } } }
感謝各位的閱讀!關(guān)于“Android編程如何實現(xiàn)短信收發(fā)及語音播報提示功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。