Android:SMS TEL MAIL 使用集合

Android发送短信、打电话、发送邮件的程序集合。

短信发送模式包括:
1.使用SMSManager发送短信,发送的短信不存于“信息”中。
2.使用ContentResolver发送短信,短信存放于“信息”中。
3.使用Intent发送短信,调用系统的“信息”程序发送。

打电话模式包括:
1.调用空的Dial拔号。
2.调用Dial并传递号码。
3.直拔。

发送邮件包括:
1.发送普通邮件。
2.发送附件。

package lab.sodino.stm;  
import android.app.Activity;  
import android.content.ContentResolver;  
import android.content.ContentValues;  
import android.content.Intent;  
import android.net.Uri;  
import android.os.Bundle;  
import android.telephony.gsm.SmsManager;  
import android.view.View;  
import android.widget.Button;  
import android.widget.Toast;  
public class STMAct extends Activity {  
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        ((Button) findViewById(R.id.btnSmsMag))  
                .setOnClickListener(new Button.OnClickListener() {  
                    public void onClick(View v) {  
                        sendSms1();  
                        Toast.makeText(STMAct.this, "已发送", Toast.LENGTH_SHORT)  
                                .show();  
                    }  
                });  
        ((Button) findViewById(R.id.btnSmsInbox))  
                .setOnClickListener(new Button.OnClickListener() {  
                    public void onClick(View v) {  
                        sendSmsInbox();  
                        Toast.makeText(STMAct.this, "已发送", Toast.LENGTH_SHORT)  
                                .show();  
                    }  
                });  
        ((Button) findViewById(R.id.btnSmsIntent))  
                .setOnClickListener(new Button.OnClickListener() {  
                    public void onClick(View v) {  
                        sendSmsIntent();  
                        Toast.makeText(STMAct.this, "已发送", Toast.LENGTH_SHORT)  
                                .show();  
                    }  
                });  
        ((Button) findViewById(R.id.btnTelEmpty))  
                .setOnClickListener(new Button.OnClickListener() {  
                    public void onClick(View v) {  
                        telDialEmpty();  
                    }  
                });  
        ((Button) findViewById(R.id.btnTelPhone))  
                .setOnClickListener(new Button.OnClickListener() {  
                    public void onClick(View v) {  
                        telDialPhone();  
                    }  
                });  
        ((Button) findViewById(R.id.btnTelCall))  
                .setOnClickListener(new Button.OnClickListener() {  
                    public void onClick(View v) {  
                        telCall();  
                    }  
                });  
        ((Button) findViewById(R.id.btnMailSendto))  
                .setOnClickListener(new Button.OnClickListener() {  
                    public void onClick(View v) {  
                        mailSendto();  
                    }  
                });  
        ((Button) findViewById(R.id.btnMailSend))  
                .setOnClickListener(new Button.OnClickListener() {  
                    public void onClick(View v) {  
                        mailSend();  
                    }  
                });  
    }  
    private void sendSms1() {  
        // 需要android.permission.SEND_SMS  
        SmsManager smsManager = SmsManager.getDefault();  
        smsManager.sendTextMessage("10086", null, "1008611", null, null);  
    }  
    private void sendSmsInbox() {  
        // 需要android.permission.READ_SMS与android.permission.WRITE_SMS  
        ContentValues values = new ContentValues();  
        values.put("address", "10086");  
        values.put("body", "bylcx");  
        ContentResolver contentResolver = getContentResolver();  
        // 实验中两者都会在信息栏中保存所发的信息。  
        contentResolver.insert(Uri.parse("content://sms/sent"), values);  
        // contentResolver.insert(Uri.parse("content://sms/inbox"), values);  
    }  
    private void sendSmsIntent() {  
        // 不需要权限,跳转到"信息"中。  
        Intent sendIntent = new Intent(Intent.ACTION_SENDTO, Uri  
                .parse("sms://"));  
        sendIntent.putExtra("address", "10086");  
        sendIntent.putExtra("sms_body", "bylcs");  
        startActivity(sendIntent);  
    }  
    private void telDialEmpty() {  
        // 不需要权限,跳转到"拔号"中。  
        Intent callIntent = new Intent(Intent.ACTION_CALL_BUTTON);  
        startActivity(callIntent);  
    }  
    private void telDialPhone() {  
        // 不需要权限,跳转到"拔号"中。  
        Intent callIntent = new Intent(Intent.ACTION_DIAL, Uri  
                .parse("tel:10086"));  
        startActivity(callIntent);  
    }  
    private void telCall() {  
        // 需要android.permission.CALL_PHONE  
        Intent callIntent = new Intent(Intent.ACTION_CALL, Uri  
                .parse("tel:10086"));  
        startActivity(callIntent);  
    }  
    private void mailSendto() {  
        // 需要android.permission.SENDTO权限  
        Uri uri = Uri.parse("mailto:10086@qq.com");  
        Intent mailIntent = new Intent(Intent.ACTION_SENDTO, uri);  
        startActivity(mailIntent);  
    }  
    private void mailSend() {  
        // 需要android.permission.SEND权限  
        Intent mailIntent = new Intent(Intent.ACTION_SEND);  
        mailIntent.setType("text/plain");  
        String[] arrReceiver = { "10086@qq.com", "10086@qq.com" };  
        String[] arrCc = { "10086@qq.com", "10086@qq.com" };  
        String[] arrBcc = { "10086@qq.com", "10086@qq.com" };  
        String mailSubject = "MailSubject";  
        String mailBody = "Mail Sodino Test";  
        String attachPath = "file:///sdcard/UCDownloads/ATest.apk";  
        mailIntent.putExtra(Intent.EXTRA_EMAIL, arrReceiver);  
        mailIntent.putExtra(Intent.EXTRA_CC, arrCc);  
        mailIntent.putExtra(Intent.EXTRA_BCC, arrBcc);  
        mailIntent.putExtra(Intent.EXTRA_SUBJECT, mailSubject);  
        mailIntent.putExtra(Intent.EXTRA_TEXT, mailBody);  
        mailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(attachPath));  
        mailIntent.setType("audio/mp3");  
        startActivity(Intent.createChooser(mailIntent, "Mail Sending..."));  
    }  

----------------------------------------------------------

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwspdp.html