举例说明android中SmsManager的用法---用来实现手机发送短信的功能

       下面举例说明如何在android中SmsManager实现手机发送短信的功能。本例可以发送两条固定内容的信息,可以免编辑,可以作为群发短信的时候使用,比如说新年的祝福短信。

程序如下:

 

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

@SuppressWarnings("deprecation")
public class A03Activity extends Activity {
 private Button b01,b02;
 private TextView tv01,tv02,tv03;
 private String strMessage;//作为要发送的短信的内容
 private static final int PICK_CONTACT_SUBACTIVITY=2;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        b01=(Button)findViewById(R.id.button01);//发送短信内容1的按钮
        b02=(Button)findViewById(R.id.button02);//发送短信内容2的按钮
        tv01=(TextView)findViewById(R.id.tv01);
        tv02=(TextView)findViewById(R.id.tv02);//作为短信内容1
        tv03=(TextView)findViewById(R.id.tv03);//作为短信内容2
        b01.setOnClickListener(new OnClickListener(){
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    //获取联系人信息
    Uri uri01=Uri.parse("content://contacts:people");
    Intent i01=new Intent(Intent.ACTION_PICK,uri01);
    strMessage=tv02.getText().toString();
    startActivityForResult(i01,PICK_CONTACT_SUBACTIVITY);    
   }         
        });
        b02.setOnClickListener(new OnClickListener(){
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    //获取联系人的信息
    Uri uri=Uri.parse("content://contacts/people");
    Intent i=new Intent(Intent.ACTION_PICK,uri);
    strMessage=tv03.getText().toString();
    startActivityForResult(i,PICK_CONTACT_SUBACTIVITY);
   }         
        });       
    }
    protected void onActivityResult(int requestCode,int resultCode,Intent data){
     switch(requestCode){
     case PICK_CONTACT_SUBACTIVITY:
      final Uri u=data.getData();
      if(u!=null){
       try {
        //必须要有android.permission.READ_CONTACTS
     Cursor c=managedQuery(u,null,null,null,null);
     //移动到联系人名单中的第一个
     c.moveToFirst();
     //获取联系人的姓名
     String name=c.getString(c.getColumnIndexOrThrow(People.NAME));
     //获取联系人的电话
     String phone=c.getString(c.getColumnIndexOrThrow(People.NUMBER));
     String address=phone;//将联系人的电话作为发送短信目的地
     System.out.println(strMessage);
     SmsManager sm=SmsManager.getDefault();
     PendingIntent pi=PendingIntent.getBroadcast(A03Activity.this, 0, new Intent(), 0);
     //发出短信
     sm.sendTextMessage(address, null, strMessage, pi, null);
     //用Toast显示短信发送情况
     Toast.makeText(A03Activity.this, R.string.str_msg+name, Toast.LENGTH_LONG).show();
     //在TextView中显示短信发送的目的地和姓名
     tv01.setText(name+":"+phone);
    } catch (IllegalArgumentException e) {
     // TODO Auto-generated catch block
     tv01.setText(e.toString());
     e.printStackTrace();
    }
       
      }
      break;
     }
     super.onActivityResult(requestCode, resultCode, data);
    }

}

在本例中需要添加两个权限:

android.permission.READ_CONTACTS和android.permission.SEND_SMS;

 

   

如果想要发送内容作为可编辑的时候,只需要把TextView改成EditText即可。具体写法如下:

strMessage=myEditText.getText().toString();

 

 

 

 

你可能感兴趣的:(android,String,null,手机,button,电话)