Call intent to open a map, open a browser, create an email, and create an event.
:
Code
Package com. example. hellointent; import java. util. calendar; import java. util. list; import org. apache. http. protocol. HTTP; import android.net. uri; import android. OS. bundle; import android. provider. calendarContract; import android. provider. calendarContract. events; import android. annotation. suppressLint; import android. app. activity; import android. content. intent; import android. content. pm. packageManager; import Droid. content. pm. resolveInfo; import android. text. textUtils; import android. view. view; import android. widget. toast; public class MainActivity extends Activity {private static int sIndex = 0; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);} // call public void onTel (View view) {Uri number = Uri. parse ("tel: 5551234"); Inten T callIntent = new Intent (Intent. ACTION_DIAL, number); String title = sIndex % 2 = 0? Null: "call"; startActivity (callIntent, title);} // call public void onMap (View view) {// Map point based on addressUri location = Uri. parse ("geo: 40.004754, 116.430867? Z = 14 "); // Or map point based on latitude/longpolling // Uri location = Uri. parse (" geo: 37.422219,-122.08364? Z = 14 "); // z param // is zoom levelIntent mapIntent = new Intent (Intent. ACTION_VIEW, location); String title = sIndex % 2 = 0? Null: "Get Map"; startActivity (mapIntent, title);} // open the browser public void onWeb (View view) {Uri webpage = Uri. parse ("http://www.android.com"); Intent webIntent = new Intent (Intent. ACTION_VIEW, webpage); String title = sIndex % 2 = 0? Null: "Open webpage"; startActivity (webIntent, title);} // create the Email public void onEmail (View view) {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 [] {"271414845@qq.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 UrisString title = sIndex % 2 = 0? Null: "-- send email --"; startActivity (emailIntent, title);} // create an event @ SuppressLint ("NewApi") public void onCreatCalender (View view) {Intent calendarIntent = new Intent (Intent. ACTION_INSERT, Events. CONTENT_URI); Calendar beginTime = Calendar. getInstance (); beginTime. set (2014, 5, 1, 9, 30); Calendar endTime = Calendar. getInstance (); endTime. set (2014, 5, 1, 18, 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"); String title = sIndex % 2 = 0? Null: "window forward Activity"; startActivity (calendarIntent, title);} private void startActivity (Intent intent, String title) {// If you invoke an intent and there is no app available on the device // that can handle the intent, your app will crash. packageManager packageManager = getPackageManager (); List
Activities = packageManager. queryIntentActivities (intent, 0); if (activities. size ()> 0) {if (TextUtils. isEmpty (title) {startActivity (intent);} else {// To show the chooser, create an Intent using createChooser () // and pass it to startActivity (). for example: // create the title Intent chooser = Intent For the selector. createChooser (intent, title); startActivity (chooser) ;}} else {Toast. makeText (this, "the interface that meets the requirements is not found", Toast. LENGTH_SHORT ). show () ;}sindex ++ ;}}
Laout File
Note:
1. PackageManager packageManager = getPackageManager ();
List Activities = packageManager. queryIntentActivities (intent, 0 );
Activities. size ()> 0
It is used to determine whether a required interface exists. If startActivity (intent) is not called directly, the program crash
2.
If activities. size () is greater than 1, the selector will appear.
// Create a title for the selector
Intent chooser = Intent. createChooser (intent, title );
StartActivity (chooser );
You can see
Title selector:
Default title Selector