扩展android内置消息传递功能

时间:2015-04-27 11:51:45

标签: android android-studio message

如何在android中扩展内置消息传递服务,以提供文本到语音转换的选项以及消息中的文本。 我的意思是我希望文本到语音转换的图标显示“Tick mark sign”,表示邮件已发送。当用户单击此图标时,文本应转换为语音。 以下代码获取android消息并将其转换为声音,但它是一个单独的应用程序,但不是内置的Android消息传递服务的扩展。 我怎么能这样做?

这是我的MainActivity.java文件

multiply

这是我的speaker.java

protected void onCreate(Bundle savedInstanceState) {
 .........

    toggleListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton view, boolean isChecked) {
            if(isChecked){
                speaker.allow(true);
                speaker.speak(getString(R.string.start_speaking));
            }else{
                speaker.speak(getString(R.string.stop_speaking));
                speaker.allow(false);
            }
        }
    };
    toggle.setOnCheckedChangeListener(toggleListener);
    checkTTS();
    initializeSMSReceiver();
    registerSMSReceiver();
}
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == CHECK_CODE){
        if(resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS){
            speaker = new Speaker(this);
        }else {
            Intent install = new Intent();
            install.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
            startActivity(install);
        }
    }
}
private void initializeSMSReceiver(){
    smsReceiver = new BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent intent) {

            Bundle bundle = intent.getExtras();
            if(bundle!=null){
                Object[] pdus = (Object[])bundle.get("pdus");
                for(int i=0;i<pdus.length;i++){
                    byte[] pdu = (byte[])pdus[i];
                    SmsMessage message = SmsMessage.createFromPdu(pdu);
                    String text = message.getDisplayMessageBody();
                    String sender = getContactName(message.getOriginatingAddress());
                    speaker.pause(LONG_DURATION);
                    speaker.speak("You have a new message from" + sender + "!");
                    speaker.pause(SHORT_DURATION);
                    speaker.speak(text);
                    smsSender.setText("Message from " + sender);
                    smsText.setText(text);
                }
            }

        }
    };
}
private String getContactName(String phone){
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone));
    String projection[] = new String[]{ContactsContract.Data.DISPLAY_NAME};
    Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
    if(cursor.moveToFirst()){
        return cursor.getString(0);
    }else {
        return "unknown number";
    }
}

private void registerSMSReceiver() {
    IntentFilter intentFilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
    registerReceiver(smsReceiver, intentFilter);
}

0 个答案:

没有答案