android 发短信 调到联系人

原文找不到了。看了别人的一些。自己加了一点。做一个代码备份。自己看就可以了。先上一下图吧。看看一效果。

android 发短信 调到联系人_第1张图片

通过上边选择联系人。把选择过的联系显示到EditView 中。而发送短信时。可以获取联系人的电话号码。

发短信的的类SendmessageActivity.java

package com.hkrt.action;

import java.util.List;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.ContentResolver;
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.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SendmessageActivity extends Activity {
	private EditText editNum;
	private EditText editContext;
	private String phoneNum;
	private int LIANXR=10;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        editNum =(EditText)this.findViewById(R.id.input_num);
        editContext =(EditText)this.findViewById(R.id.input_content);
        Button button =(Button)this.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
//				String phoneNum = editNum.getText().toString();
				String context = editContext.getText().toString();
				SmsManager sms = SmsManager.getDefault();//发送信息的管理器
				PendingIntent sentIntent = PendingIntent.getBroadcast(SendmessageActivity.this, 0, new Intent(), 0);//PendingIntent也是一个意图
				//把短信内容进行每70个字节分割
				List<String> conts= sms.divideMessage(context);
				for(String str:conts){
					sms.sendTextMessage(phoneNum, null, str, sentIntent, null);
				}
				//信息发送完后提示
			   Toast.makeText(SendmessageActivity.this, "短信发送完成", Toast.LENGTH_LONG).show();
			}
		});
        Button btnContact  = (Button)this.findViewById(R.id.btnContact);
        btnContact.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				 Intent intent = new Intent(Intent.ACTION_PICK,  ContactsContract.Contacts.CONTENT_URI);

			        startActivityForResult(intent, LIANXR);
			}
		});
        
        
        
    }
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		 ContentResolver resol = getContentResolver();
		if (resultCode == Activity.RESULT_OK) {
			String name;
			Uri contactData = data.getData();
			Cursor c = managedQuery(contactData, null, null, null, null);
			c.moveToFirst();
			name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
			editNum.setText(name);
			  // 取得联系人id,每个条目都有一个唯一的id(主键)
            String contactId = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
			  // 取得联系人的号码
            Cursor phone = resol.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
            while (phone.moveToNext()) {
               phoneNum = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
               System.out.println("phonum:"+phoneNum);
            }

		}
		
		
		
		
	}
	
 
	
}
main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
    <EditText 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/input_num"
    />
     <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="contact"
        android:id="@+id/btnContact"
        />
    <TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/input_context"
     />
     <EditText 
     	android:layout_height="wrap_content"
     	android:layout_width="fill_parent"
     	android:id="@+id/input_content" 
     	android:minLines="3"
     	android:gravity="top"
     	
     	/>
    <Button 
     android:text="@string/button"
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/button"
    />
   
</LinearLayout>
调用联系人ContactPick.java

package com.hkrt.action;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class ContactPick extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Intent orgIntent = getIntent();
		Uri queryUri = orgIntent.getData();
		final Cursor c = managedQuery(queryUri,null,null,null,null);
		String[] fromColumns = new String[] { ContactsContract.Contacts.DISPLAY_NAME };
		int[] toLayoutIDs = new int[] { R.id.itemTextView };
		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.listitemlayout, c, fromColumns, toLayoutIDs);
		ListView lv = (ListView) findViewById(R.id.contactListView);
		lv.setAdapter(adapter);
		lv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int pos,long id) {
				c.moveToPosition(pos);
				int rowId = c.getInt(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
				Uri outURI = Uri.parse(ContactsContract.Contacts.CONTENT_URI .toString()+ rowId);
				Intent outData = new Intent();
				outData.setData(outURI);
				setResult(Activity.RESULT_OK, outData);
				finish();
			}
		});

	}

}

对应的两个xml .contact.xml 和listitemlayout.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<ListView 
	    android:id="@+id/contactListView"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content" />
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView 
    android:id="@+id/itemTextView" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:padding="10px"
    android:textSize="16px" 
    android:textColor="#FFF" />

</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.hkrt.action"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />
  
    <application android:icon="@drawable/send" android:label="@string/app_name">
    <uses-library android:name="android.test.runner" />
        <activity android:name=".SendmessageActivity"  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
	       <activity android:name=".ContactPick" android:label="@string/app_name">
	            <action android:name="android.intent.action.PICK" />
	            <category android:name="android.intent.category.DEFAULT" />
	        </activity>
    </application>
    <instrumentation android:name="android.test.InstrumentationTestRunner"
  android:targetPackage="com.hkrt.action" android:label="Tests for My App" />
     <uses-permission android:name="android.permission.SEND_SMS"/>
     <uses-permission android:name="android.permission.READ_CONTACTS" />
     <uses-permission android:name="android.permission.WRITE_CONTACTS" />
     
</manifest>

demo 目录图:

android 发短信 调到联系人_第2张图片




你可能感兴趣的:(android 发短信 调到联系人)