Android Intent隐式启动自定义意图

这是Android高级编程上面的一个例子,先看一下程序的效果:

ApiDemo

public class ContackPickerTest extends Activity {
	private static final int REQUEST_CONTACT_PICKER = 1;

	private Button mContactPickerBtn;
	private TextView mTextView;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contact_picker_test);

		mContactPickerBtn = (Button) findViewById(R.id.contact_picker_button);
		mTextView = (TextView) findViewById(R.id.textView);

		mContactPickerBtn.setOnClickListener(listener);
	};

	View.OnClickListener listener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(Intent.ACTION_PICK,
					Uri.parse("content://contactsr"));
			startActivityForResult(intent, REQUEST_CONTACT_PICKER);
		}
	};

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case REQUEST_CONTACT_PICKER: {
			if (resultCode == Activity.RESULT_OK) {
				showContactName(data);
			}
		}
			break;

		default:
			break;
		}
	}

	private void showContactName(Intent data) {
		Uri contactData = data.getData();
		Cursor cursor = managedQuery(contactData, null, null, null, null);
		cursor.moveToFirst();
		String name = cursor.getString(cursor
				.getColumnIndexOrThrow(People.NAME));
		mTextView.setText(name);
	};
}
public class ContactPicker extends Activity {
	private ListView mListView;
	private SimpleCursorAdapter mCursorAdapter;
	private Cursor mCursor;

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

		mCursor = getContactCursor(getIntent());

		mListView = (ListView) findViewById(R.id.listView);
		String[] from = new String[] { People.NAME };
		int[] to = new int[] { R.id.textView };
		mCursorAdapter = new SimpleCursorAdapter(this,
				R.layout.contact_picker_item, mCursor, from, to);
		mListView.setAdapter(mCursorAdapter);
		mListView.setOnItemClickListener(listener);
	}

	OnItemClickListener listener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView parent, View view, int position,
				long id) {
			mCursor.moveToPosition(position);
			int rowID = mCursor.getInt(mCursor.getColumnIndexOrThrow("_id"));
			Uri uri = Uri.parse(getContactData(getIntent()).toString() + rowID);
			Intent intent = new Intent();
			intent.setData(uri);
			setResult(Activity.RESULT_OK, intent);
			finish();
		}
	};

	private Cursor getContactCursor(Intent intent) {
		Cursor cursor;
		Uri uri = getContactData(intent);
		cursor = managedQuery(uri, null, null, null, null);
		return cursor;
	}

	private Uri getContactData(Intent intent) {
		Cursor cursor = null;
		String dataPath = intent.getData().toString();
		dataPath = dataPath.substring(0, dataPath.length() - 1);
		Uri uri = Uri.parse(dataPath + "/people/");
		return uri;
	}
}

在这个从程序中如果写成Intent intent=new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));程序会出现错误,而如果写成Uri.parse("content://contacts/people")则会匹配系统自己的联系人列表,所以没有办法在Uri.parse("content://contactsr")上填了个r在manifest里面也相应的添了个r,然后再将这个r去掉。

暂无评论
  • 1:请一针见血的评论。
  • 2:评论需要审核通过后才能显示。
  • 3:评论字数限制在1000字以内。
  • 当前字数:0
热门文章
推荐文章
随机文章
关于本站 - 广告服务 - 版权声明 - 联系我们 - 友情链接 - 网站地图 - 帮助中心