随笔 - 23  文章 - 0 评论 - 14 trackbacks - 0

直奔主题~!

结构如图:

person_content.java代码:

public class person_content extends ListActivity {

	private Button submit_result_btn;
	private HashMap<String, String> al;

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		CheckBox cb = (CheckBox) v.findViewById(R.id.cb);
		TextView username_txt = (TextView) v.findViewById(R.id.username);
		TextView phonenum_txt = (TextView) v.findViewById(R.id.tel_num);
		String username = username_txt.getText().toString();
		String phonenum = phonenum_txt.getText().toString();
		cb.setEnabled(true);
		if (cb.isChecked()) {

			cb.setChecked(false);
			al.remove(phonenum);

		} else {
			cb.setChecked(true);
			al.put(phonenum, username);

		}
		cb.setEnabled(false);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.show_contacts);
		bind();
	}

	public void bind() {

		al = new HashMap<String, String>();
		submit_result_btn = (Button) this.findViewById(R.id.submit_result_btn);
		Cursor ps_cs = getContentResolver().query(People.CONTENT_URI, null,
				null, null, null);
		startManagingCursor(ps_cs);
		String[] string = { Phones.NAME, Phones.NUMBER };
		int[] intText = { R.id.username, R.id.tel_num };
		SimpleCursorAdapter sca = new SimpleCursorAdapter(person_content.this,
				R.layout.person_content, ps_cs, string, intText);
		setListAdapter(sca);
		submit_result_btn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub

				Intent it = new Intent();
				Bundle bd = new Bundle();
				bd.putSerializable("select_people", al);
				it.putExtras(bd);
				setResult(1, it);
				finish();

			}
		});

	}

}

Sms_Send_AllActivity.java代码:

public class Sms_Send_AllActivity extends Activity {

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		// super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 0) {
			if (resultCode == 1) {
				Bundle bd = data.getExtras();
				al = (HashMap<String, String>) bd
						.getSerializable("select_people");

			}
		}
	}

	private Button select_btn;
	private Button send_btn;
	private EditText content_txt;
	private HashMap<String, String> al;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		findAll();
		bind();
	}

	public void findAll() {
		select_btn = (Button) this.findViewById(R.id.People_btn);
		send_btn = (Button) this.findViewById(R.id.submit_btn);
		content_txt = (EditText) this.findViewById(R.id.content_txt);

	}

	public void bind() {
		select_btn.setOnClickListener(mylistener);
		send_btn.setOnClickListener(mylistener);
	}

	private View.OnClickListener mylistener = new OnClickListener() {

		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.People_btn:
				Intent it = new Intent(Sms_Send_AllActivity.this,
						person_content.class);
				startActivityForResult(it, 0);
				break;
			case R.id.submit_btn:
				String content = content_txt.getText().toString();
				SmsManager sm = SmsManager.getDefault();
				for (Iterator iter = al.entrySet().iterator(); iter.hasNext();) {
					Map.Entry element = (Entry) iter.next();
					String number = element.getKey().toString();
					// 这句目的是为了在模拟器上真实模拟
					number = number.replace("-", "").trim();
					String name = element.getValue().toString();
					PendingIntent sentIntent = PendingIntent.getBroadcast(
							Sms_Send_AllActivity.this, 0, new Intent(), 0);
					sm.sendTextMessage(number, null, content, sentIntent, null);

				}

				Toast.makeText(Sms_Send_AllActivity.this, "发送成功",
						Toast.LENGTH_LONG).show();
				break;
			default:
				break;
			}
		}
	};
}

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">
	<LinearLayout android:layout_height="wrap_content"
		android:layout_width="fill_parent" android:id="@+id/linearLayout1">
		<Button android:text="select_People" android:id="@+id/People_btn"
			android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
	</LinearLayout>
	<LinearLayout android:layout_height="wrap_content"
		android:layout_width="fill_parent" android:id="@+id/linearLayout2">
		<TextView android:text="content" android:id="@+id/textView1"
			android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
		<EditText android:layout_height="wrap_content"
			android:layout_weight="1" android:layout_width="wrap_content"
			android:id="@+id/content_txt" android:inputType="textMultiLine">
			<requestFocus></requestFocus>
		</EditText>
	</LinearLayout>
	<Button android:text="send" android:id="@+id/submit_btn"
		android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>

person_content.xml代码:

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

<LinearLayout android:layout_height="wrap_content"
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent" android:id="@+id/linearLayout1"
	android:descendantFocusability="blocksDescendants">
	<CheckBox android:text="select" android:id="@+id/cb"
		android:layout_width="wrap_content" android:layout_height="wrap_content"
		android:focusable="false" android:focusableInTouchMode="false"
		android:enabled="false"></CheckBox>
	<TextView android:id="@+id/username" android:layout_width="wrap_content"
		android:layout_height="wrap_content"></TextView>
	<TextView android:id="@+id/tel_num" android:layout_width="wrap_content"
		android:layout_height="wrap_content"></TextView>
</LinearLayout>

show_contacts.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:layout_height="wrap_content" android:id="@id/android:list"
		android:layout_width="fill_parent"></ListView>
	<Button android:text="submit" android:id="@+id/submit_result_btn"
		android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

</LinearLayout>

androidManifest.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
	package="Sms_Send_All.Jason" android:versionCode="1"
	android:versionName="1.0">
	<uses-sdk android:minSdkVersion="4" />
	<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
	<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>

	<application android:icon="@drawable/icon" android:label="@string/app_name">
		<activity android:name=".Sms_Send_AllActivity" 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="person_content"></activity>

	</application>
</manifest>
posted on 2011-09-16 17:38 Jwc 阅读(...) 评论(...) 编辑 收藏