Android UI(4)Getting Started - Interacting with Other Apps

Interacting with Other Apps
Every time we pass an intent to the system, startActivity, even the system will start an activity in other apps.

1. Sending the User to Another App
I do not need to build a map on my own apps if I meet an address and I want to display it on a map. I can send an intent to other apps and pass the address as parameter. And we need to use implicit Intent

Build an Implicit Intent
For example:
Phone Call
Uri number = Uri.parse("tel:5127922045");
Intent callIntent = new Intent(Intent.ACTION_DIAL, number):

Once I call the startActivity(), the Phone app initiates a call to the given phone number then.

View a Map
Uri location = Uri.parse("geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California");
//We can directly put the address in the query with geo 0
// Uri location = Uri.parse("geo:37.422219,-122.08364?z=14");
// Or we can also the latitude and longitude with z , zoom level
Intent mapIntent = new Intent(Intent.ACTION_VIEW, location);

View a Web Page
Uri webpage = Uri.parse("http://www.android.com");
Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);

Email with Extra Data
Intent emailIntent = new Intent(Intent.ACTION_SEND);

emailIntent.setType(HTTP.PLAIN_TEXT_TYPE);
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"luohuazju@gmail.com"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT,"Subject Title");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Content with Good Luck!");
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://path/to/email/attachment");

Calendar Event
Intent calendarIntent = new Intent(Intent.ACTION_INSERT, Events.CONTENT_URI);
Calendar beginTime = Calendar.getInstance().set(2012,0,19,7,30);
Calendar endTime = Calendar.getInstance().set(2012,0,19,10,30);
calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis());
calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis());
calendarIntent.putExtra(Events.TITLE, "Meeting Plan");
calendarIntent.putExtra(Events.EVENT_LOCATION, "everywhere");

Verify There is an App to Receive the Intent
Try to query and find the activities which will response to your intent

PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent,0);
boolean isIntentSave = activities.size() > 0;

Here is what I wrote in my activity.
intent.setClass(this, PersonListActivity.class);if(isIntentSafe(intent)){ startActivity(intent);

}


private boolean isIntentSafe(Intent intent) {
boolean flag = false; PackageManager manager = this.getPackageManager(); List<ResolveInfo> activities = manager.queryIntentActivities(intent, 0); if(activities != null && activities.size() > 0){
Log.d(TAG,"The intent is save to invoke."); flag = true; } return flag;

}


Start an Activity with the Intent
Just call startActivity(intent);
If multiple activities are responding this intent, the system will provide an option page.

Show an App Chooser
Only we can customized the title of the chooser
Intent intent = new Intent(...);
String title = getResources().getText(R.string.chooser_title);
Intent chooser = Intent.createChooser(intent,title);
startActivity(chooser);

2. Getting a Result from an Activity
For example, your app can start a camera app and receive the captured photo as as a result. Or start the People app in order to pick up an contract detail.
startActivityForResult() -----> onActivityResult() callback

Start the Activity
private static final int REQUEST_CODE = 10001;
final Button buttonJson = (Button) findViewById(R.id.pickup_contact);
buttonJson.setOnClickListener(new View.OnClickListener() { publicvoid onClick(View v) { Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts")); pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers startActivityForResult(pickContactIntent, REQUEST_CODE); }

});


Receive the Result
protectedvoid onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == REQUEST_CODE) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// The user picked a contact.
// The Intent's data Uri identifies which contact was selected.
Log.d(TAG, "I get here! I get the callback response."); Uri contactUri = data.getData(); String[] projection = {Phone.NUMBER}; Cursor cursor = getContentResolver().query(contactUri, projection, null, null, null); cursor.moveToFirst(); int column = cursor.getColumnIndex(Phone.NUMBER); String number = cursor.getString(column); Log.d(TAG, "The number you choose it is = " + number); final TextView infoView = (TextView) findViewById(R.id.contact_info); infoView.setText("Number You Pickup is " + number); } }

}

Error Message:
04-04 10:56:42.623: E/DatabaseUtils(145): java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/data/5 from pid=711, uid=10041 requires android.permission.READ_CONTACTS

Solution:
<uses-permission android:name="android.permission.READ_CONTACTS" />


3. Allowing Other Apps to Start Your Activity
…snip...


References:
http://developer.android.com/training/basics/intents/index.html

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. 原型模式 - 通过复制生成实例
  2. 服务端开发指南与最佳实战 | 数据存储技
  3. 「Jenkins」- 使用接口批量创建任务
  4. Day-3
  5. 服务端开发指南与最佳实战 | 数据存储技
  6. AssemblyScript 入门指南[每日前端夜话0x
  7. 计算系统基础(一)
  8. 选择合适的数据存储方案
  9. 全球开发者报告:1100万开发人员积极使用 J
  10. JavaScript测试教程–part 4:模拟 API 调