---
title: "Android UI(4)Getting Started - Interacting with Other Apps"
author: "sillycat"
date: "2013-04-05T00:22:07Z"
tags: []
categories: ["移动开发"]
slug: "android-ui-4-getting-started-interacting-with-other-apps"
draft: false
lastmod: "2013-04-05T00:22:07Z"
id: 1842143
url: "https://sillycat.iteye.com/blog/1842143"
---

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 activities = packageManager.queryIntentActivities(intent,0); boolean isIntentSave = activities.size() &gt; 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 activities = manager.queryIntentActivities(intent, 0); if(activities != null && activities.size() &gt; 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() -----&gt; 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: 3. Allowing Other Apps to Start Your Activity …snip... References: http://developer.android.com/training/basics/intents/index.html