Sunday, October 6, 2013

Android-Intent Filters

Intent Filters

Using Intent Filters

Earlier, you saw how an activity can invoke another activity using the Intent object. In order for other activities to invoke your activity, you need to specify the action and category within the
element in the AndroidManifest.xml file, like this:
This is a very simple example in which one activity calls another using the “net.learn2develop.ACTIVITY2” action. The following Try It Out shows you a more sophisticated example.  Specifying Intent

Intent Filters

Filters in More Details
1. Using the Intents project created earlier, add a new class to the project and name it MyBrowserActivity.java. Also add a new XML file to the res/layout folder and name it browser.xml

Intent Filters

2. Add the following statements in bold to the AndroidManifest.xml file:

package=”net.learn2develop.Intents”
android:versionCode=”1”
android:versionName=”1.0”>
android:icon=”@drawable/icon” android:label=”@string/app_name”>
android:label=”@string/app_name”>

android:label=”@string/app_name”>

Intent Filters

3. Add the following statements in bold to the main.xml file:
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent” >

Intent Filters

4. Add the following statements in bold to the MainActivity.java file:
package net.learn2develop.Intents;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button b1, b2, b3, b4, b5;
int request_Code = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//---Web browser button---
b1 = (Button) findViewById(R.id.btn_webbrowser);
b1.setOnClickListener(new OnClickListener()
{
//...
});
//---Make calls button---
b2 = (Button) findViewById(R.id.btn_makecalls);
b2.setOnClickListener(new OnClickListener()
{
//...
});
//---Show Map button---
b3 = (Button) findViewById(R.id.btn_showMap);
b3.setOnClickListener(new OnClickListener()
{
//...
});
//---Choose Contact button---
b4 = (Button) findViewById(R.id.btn_chooseContact);
b4.setOnClickListener(new OnClickListener()
{
//...
});
b5 = (Button) findViewById(R.id.btn_launchMyBrowser);
b5.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
Intent i = new
Intent(“net.learn2develop.MyBrowser”);
i.setData(Uri.parse(“http://www.amazon.com”));
startActivity(i);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
//...
}
}

Intent Filters

5. Add the following statements in bold to the browser.xml file:

android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent” >
android:id=”@+id/WebView01”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content” />



6. Add the following statements in bold to the MyBrowserActivity.java file:
package net.learn2develop.Intents;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MyBrowserActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.browser);
Uri url = getIntent().getData();
WebView webView = (WebView) findViewById(R.id.WebView01);
webView.setWebViewClient(new Callback());
webView.loadUrl(url.toString());
}
private class Callback extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return(false);
}
}
}

Intent Filters

6. Press F11 to debug the application on the Android Emulator.
7. Click the Launch my Browser button and you should see the new activity displaying the Amazon.com web page (see Figure).

Intent Filters

How It Works
In this example, you created a new activity named MyBrowserActivity. You first needed to declare it in the AndroidManifest.xml file:
android:label=”@string/app_name”>

Intent Filters

In the element, you declared it to have two actions, one category, and one data. This means that all other activities can invoke this activity using either the “android.intent.action.VIEW” or the “net.learn2develop.MyBrowser” action. For all activities that you want others to call using the start Activity() or startActivityForResult() methods, they need to have the “android.intent.category
.DEFAULT” category. If not, your activity will not be callable by others. The element specifies the type of data expected by the activity. In this case, it expects the data to start with the “http://” prefix.
The preceding intent filter could also be rewritten as follows:
android:label=”@string/app_name”>
Writing the intent filter this way makes it much more readable and logically groups the action, category,and data within an intent filter.
If you now use the ACTION_VIEW action with the data shown here, Android will display a selection (as shown in Figure):
Intent i = new
Intent(android.content.Intent.ACTION_VIEW,
Uri.parse(“http://www.amazon.com”));
startActivity(i);
You can choose between using the Browser application or the Intents application that you are currently building.















Upload UIImage as base64 String

Upload UIImage as Base64 String (Upload UIImage as string) //Select Pic From Camera or Gallery       @objc func btnPro...