Overview
I hope you got a chance to read some useful articles to start programming in Android application. Given below are some of the links:
So far, I have explained
here about the use of an intent object to call other activities. Let's just recap and gain some more detailed understanding on how the Intent object performs its magic.
Introduction
In the article
Intent in Android, we learned that we can call another activity bypassing its action to the constructor of an Intent object
- startActivity(new Intent(“com.example.administrator.Fragment2Activity”));
This action is known as component and it is used to identify the target activity/application that you want to invoke. It can be rewritten by specifying the class name of the activity if it resides in the project, like
- Intent intent=new Intent(this, MySecondActivity.class);
- startActivity(intent);
You can also create an Intent object by passing in an action constant and data, as follows
- Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.co.in"));
- startActivity(intent);
The action portion defines what you want to do, while the data portion contains the data for the target activity to act upon. It can further extend and pass the data to the Intent object using the setData method.
- Intent intent=new Intent(Intent.ACTION_VIEW”); intent.setData(Uri.parse("http://www.google.co.in"));
In the above example, it is indicated that you want to view a web page with the specified URL. The Android OS will look for all activities that are able to satisfy your request. This process is known as intent resolution.
Coding
So, we have already seen how an activity can invoke another activity using the Intent object. Here, I will discuss the Intent filters. Here, I added some code in the existing app which I have covered in the previous article. Added a blank activity called BrowserActivity and code in AndroidManifest.xml file
- <activity
- android:name=".BrowserActivity"
- android:label="@string/title_activity_browser" >
- <intent-filter>
- <action android:name="android.intent.action.VIEW"/>
- <action android:name="com.example.administrator.Browser" />
- <category android:name="android.intent.category.DEFAULT"/>
- <data android:scheme="http"/>
- </intent-filter>
- </activity>
The button is added to the activity_main.xml file.
- <Button
- android:id="@+id/buttonlaunchbrowser"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Launch C# Corner"
- android:onClick="onClickLaunchBrowser"
- />
onClick() method is defined in the MainActivity.java file.
- public void onClickLaunchBrowser(View view){
- Intent intent = new Intent("com.example.administrator.Browser");
- intent.setData(Uri.parse("http://www.c-sharpcorner.com"));
- startActivity(intent);
- }
Add a WebView control in the activity_browser.xml file.
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- tools:context="com.example.administrator.myfragmentapp.BrowserActivity">
- <WebView
- android:id="@+id/webview1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
Add code for WebView control in the BrowserActivity.java class.
- package com.example.administrator.myfragmentapp;
-
- import android.app.Activity;
- import android.net.Uri;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.webkit.WebView;
- import android.webkit.WebViewClient;
- public class BrowserActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_browser);
-
- Uri uri=getIntent().getData();
- WebView webview=(WebView)findViewById(R.id.webview1);
- webview.setWebViewClient(new Callback());
- webview.loadUrl(uri.toString());
- }
- private class Callback extends WebViewClient {
- @Override
- public boolean shouldOverrideUrlLoading
-
- (WebView webview, String url){
- return(false);
- }
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
-
- getMenuInflater().inflate(R.menu.menu_browser, menu);
- return true;
- }
-
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
-
-
-
- int id = item.getItemId();
-
-
- if (id == R.id.action_settings) {
- return true;
- }
-
- return super.onOptionsItemSelected(item);
- }
- }
Explanation
In the above code, a new named BrowserActivity is created. So first need to declare it in the AndroidManifest.xml file.
- <activity
- android:name=".BrowserActivity"
- android:label="@string/title_activity_browser" >
- <intent-filter>
- <action android:name="android.intent.action.VIEW"/>
- <action android:name="com.example.administrator.Browser" />
- <category android:name="android.intent.category.DEFAULT"/>
- <data android:scheme="http"/>
- </intent-filter>
- </activity>
In the <intent-filter> element, there are two actions, category, and data. This means that all the other activities can invoke this activity using either the “android.intent.action.VIEW” or the “com.example.administrator.Browser” action. For all activities that you want others to call by using the startActivity(), they need to have the “android.intent.category.DEFAULT” category. If it is not defined, your activity will not be callable by others. The <data> element specifies the type of data expected by the activity. In this case, it expects the data to start with the http:// prefix.
The above <intent-filter> can be rewritten as,
- <activity
- android:name=".BrowserActivity"
- android:label="@string/title_activity_browser" >
- <intent-filter>
- <action android:name="android.intent.action.VIEW"/>
- <category android:name="android.intent.category.DEFAULT"/>
- <data android:scheme="http"/>
- </intent-filter>
- <intent-filter>
- <action android:name="com.example.administrator.Browser" />
- <category android:name="android.intent.category.DEFAULT"/>
- <data android:scheme="http"/>
- </intent-filter>
- </activity>
Above <intent-filter> is more readable and it logically groups the action, category, and data within an Intent filter.
If you use the ACTION_VIEW action with the data, Android will ask for a selection.
- Intent intent = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("http://www.c-sharpcorner.com"));
If multiple activities match your Intent object, then a dialog called “Complete action using” appears. So in this case, it can be customized by using the createChooser()method from the Intent class.
- Intent intent = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("http://www.c-sharpcorner.com"));
-
- startActivity(Intent.createChooser(intent,”Open Browser Using!!”));
The preceding will change the dialog title to “Open Browser Using!!” instead of action to default open the browser.
We can group our activities into categories by using the <category> element in the intent filter.
- <activity
- android:name=".BrowserActivity"
- android:label="@string/title_activity_browser" >
- <intent-filter>
- <action android:name="android.intent.action.VIEW"/>
- <action android:name="com.example.administrator.Browser" />
- <category android:name="android.intent.category.DEFAULT"/>
- <category android:name="android.intent.category.LearnApps"/>
- <data android:scheme="http"/>
- </intent-filter>
- </activity>
In this case, the below code will directly invoke the Browser activity.
- Intent intent = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("http://www.c-sharpcorner.com"));
-
- Intent.addCategory(“android.intent.category.LearnApps”);
-
- startActivity(Intent.createChooser(intent,”Open Browser Using!!”));
You added the category to the intent object using the addCategory() method. If you just omit the addCategory() statement, the preceding code will invoke the Browser activity because it will still match the default category which is android.intent.category.DEFAULT. But, if you specify a category that doesn’t match the category defined in the intent filter, it will not work and no activity will be displayed.
- Intent intent = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("http://www.c-sharpcorner.com"));
-
- Intent.addCategory(“android.intent.category.ContentProviderApps”);
-
- startActivity(Intent.createChooser(intent,”Open Browser Using!!”));
In the preceding, code (android.intent.category.ContentProviderApps) doesn’t match any category in the intent filter. Thus, a run-time exception will be raised.
So, you have to add the following category in the intent filter of Browser activity, then the preceding code will work
- <activity
- android:name=".BrowserActivity"
- android:label="@string/title_activity_browser" >
- <intent-filter>
- <action android:name="android.intent.action.VIEW"/>
- <action android:name="com.example.administrator.Browser" />
- <category android:name="android.intent.category.DEFAULT"/>
- <category android:name="android.intent.category.LearnApps"/>
- <category android:name="android.intent.category.ContentProviderApps"/>
- <data android:scheme="http"/>
- </intent-filter>
- </activity>
Thus, you can add multiple categories to an Intent object.
- Intent intent = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("http://www.c-sharpcorner.com"));
-
- Intent.addCategory(“android.intent.category.LearnApps”);
-
- Intent.addCategory(“android.intent.category.ContentProviderApps”);
-
- startActivity(Intent.createChooser(intent,”Open Browser Using!!”));
It is evident that when using an Intent object with categories, all categories added to the Intent object must fully match with those defined in the intent filter before an activity can be invoked.
Result
Run the application by pressing button Shift + F10 and get the below screenshot.
Click on the button <Lauch C# Corner>, it displays as below screenshot.
Conclusion
In the above article, I discussed the intent object and intent filters along with categories that are defined in the AndroidMenifest.xml file. I will explain about displaying notifications in the next article. If you have any questions or comments, post me a message in the C# Corner comments section.