When we develop Android apps, we need to start other apps to process some logic. For example, we need to call the system or related Map App based on an address, in this way, we do not need to write the corresponding functions in our own apps, but send some requests through Intent and call the relevant applications to process these requests. This kind of Intent is called an implicit Intent, which is relative to an explicit Intent. Explicit Intent is familiar to us. explicit Intent often needs to declare the class name, while implicit Intent needs to declare an Action, the Action defines the request we want to process. There is also data associated with the Action, such as the address we need to view, the phone number we need to call, or the email address we need to send the mail, etc. For example:
Uri number = Uri.parse("tel:5551234");Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
In the above Code, we call the Phone App through startActivity () and call (5551234. Of course, there are many examples here, which are extracted from the API documentation. Paste it here for your reference.
View map:
// Map point based on addressUri location = Uri.parse("geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California");// Or map point based on latitude/longitude// Uri location = Uri.parse("geo:37.422219,-122.08364?z=14"); // z param is zoom levelIntent mapIntent = new Intent(Intent.ACTION_VIEW, location);
Webpage Browsing:
Uri webpage = Uri.parse("http://www.android.com");Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);Send Email:
Intent emailIntent = new Intent(Intent.ACTION_SEND);// The intent does not have a URI, so declare the "text/plain" MIME typeemailIntent.setType(HTTP.PLAIN_TEXT_TYPE);emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"jon@example.com"}); // recipientsemailIntent.putExtra(Intent.EXTRA_SUBJECT, "Email subject");emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message text");emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://path/to/email/attachment"));// You can also attach multiple items by passing an ArrayList of UrisCreate an event notification:
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, "Ninja class");calendarIntent.putExtra(Events.EVENT_LOCATION, "Secret dojo");
In addition, we should define our Intent as detailed as possible. For example, if we want to call the image viewer of the system to browse images, we should define the MIME type "image /*. "To prevent the map app from being started for viewing. And without an app to respond to our request, our app will crash.
Therefore, to prevent the application from unexpectedly exiting due to the absence of other intent responses sent by our app, we verify the settings before sending the intent. To verify whether an app Activity responds to our Intent request, we need to call queryIntentActivities () for verification. This method returns a list, which is verified by checking whether the list is empty. In this way, we can safely use Intent to implement Activity interaction between different apps. If there is no response to Acitivty, we can provide some links for users to download and install. The verification method is as follows:
PackageManager packageManager = getPackageManager();
List
activities = packageManager.queryIntentActivities(intent, 0);
boolean isIntentSafe = activities.size() > 0;
In the following example, we use an implicit Intent for interaction between activities in different apps.
First, we create the first project lateral end. We create a button, create an Intent in the event, set the Action and type, and add onActivityResult () to receive data from the second application.
public void button(View view) {Intent intent = new Intent();intent.setAction(Intent.ACTION_VIEW);intent.setType("test/");startActivityForResult(intent, 2);}
@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);switch (requestCode) {case 2:switch (resultCode) {case Activity.RESULT_OK:text.setText("URI:" + data.getDataString());break;}break;}}
Step 2: Create the second application and configure Intent-filter in the configuration file;
Step 3: Listen to the return button in the second Activity and return data.
@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {switch (keyCode) {case KeyEvent.KEYCODE_BACK:Intent result = new Intent("com.example.appsend",Uri.parse("content://result_uri"));setResult(Activity.RESULT_OK, result);finish();break;}return super.onKeyDown(keyCode, event);}Case study: http://download.csdn.net/detail/huangyabin001/7561309click to open the link