SmsManager发送短信
1,
//获取SMSManager管理器 smsmanager=SmsManager.getDefault();
2,
//创建一个PendingIntent对象 PendingIntent pi=PendingIntent.getActivity(MainActivity.this, 0, new Intent(), 0); //发送短信 smsmanager.sendTextMessage(number.getText().toString(), null, content.getText().toString(), pi, null);
本程序可以直接填入对方号码,输入内容发送短信,也可以点击通讯录按钮,从通讯录中获取联系人电话
MainActivity.java
package com.hust.sendsms; import android.app.Activity; import android.app.PendingIntent; import android.content.CursorLoader; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.ContactsContract; import android.telephony.SmsManager; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { SmsManager smsmanager=null; EditText number,content; Button send; final int PICK_CONTACT=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); number=(EditText) findViewById(R.id.editText1); content=(EditText) findViewById(R.id.editText2); send=(Button) findViewById(R.id.button1); //获取SMSManager管理器 smsmanager=SmsManager.getDefault(); send.setOnClickListener(new OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub //创建一个PendingIntent对象 PendingIntent pi=PendingIntent.getActivity(MainActivity.this, 0, new Intent(), 0); //发送短信 smsmanager.sendTextMessage(number.getText().toString(), null, content.getText().toString(), pi, null); content.setText(""); Toast.makeText(MainActivity.this, "短信发送成功!", Toast.LENGTH_LONG).show(); } }); // 通讯录按钮 Button btn=(Button) findViewById(R.id.button2); btn.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub //创建intent Intent intent=new Intent(); //设置Intent的Action属性 intent.setAction(Intent.ACTION_GET_CONTENT); //设置Intent的Type属性 intent.setType("vnd.android.cursor.item/phone"); //启动Activity,并希望获取该Activity的结果 startActivityForResult(intent,PICK_CONTACT); } }); } //重写该方法,处理返回的结果,获取联系人的电话 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); switch(requestCode) { case (PICK_CONTACT): if(resultCode==Activity.RESULT_OK){//Activity.RESULT_OK标准Activity结果 -1 //获取返回的数据 Uri contactdata=data.getData(); CursorLoader cursorloader=new CursorLoader(this, contactdata, null, null, null, null); //查询联系人信息 Cursor cursor=cursorloader.loadInBackground(); //如果查询到指定的联系人 if(cursor.moveToFirst()){ //获取联系人的Id String contactId=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); //获取联系人的名字 String name=cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); String phoneNumber="此联系人暂未输入电话号码"; //根据联系人查询该联系人的详细信息 Cursor phones=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+contactId, null, null); // if(phones.moveToFirst()){ //取出电话号码 phoneNumber=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); } phones.close(); //获取联系人电话号码 number.setText(phoneNumber); } cursor.close(); } break; } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.95" android:ems="10" android:hint="请输入号码:" android:inputType="number" > </EditText> <Button android:id="@+id/button2" android:layout_width="108dp" android:layout_height="wrap_content" android:text="通讯录" /> </LinearLayout> <EditText android:id="@+id/editText2" android:layout_width="match_parent" android:layout_height="200dp" android:ems="10" android:gravity="top|left" android:hint="请输入内容:" /> <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Send_Sms" /> </LinearLayout>