private void navigateToWebSite() { // Create an intent for viewing a specific web site Uri google = Uri.parse("http://developer.android.com"); Intent intent = new Intent(Intent.ACTION_VIEW, google); // Create a chooser intent Intent chooserIntent = Intent.createChooser(intent, "Browse with..."); // Start the chooser activity if (intent.resolveActivity(getPackageManager()) != null) { startActivity(chooserIntent); } }
The chooserIntent is optional. I'm using it to add a custom title to the chooser dialog. You can simply create the intent and call "startActivity(intent)" and let Android display the standard chooser dialog.
In this example, we just told Android to include any application that can handle the ACTION_VIEW intent. What if you have written your own application that you want to include as an option for handling this type of intent? In your application's AndroidManifest.xml file, you simply add an intent-filter within the definition of the activity.
For example, here I've added a second intent-filter to handle any applications that initiates an intent on the Intent.ACTION_VIEW with "http" requests, just like in the example above.
<activity android:label="@string/app_name" android:name=".MyOtherActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER">
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW">
<category android:name="android.intent.category.DEFAULT">
<data android:scheme="http">
</intent-filter>
</activity>
No comments:
Post a Comment