Monday, December 20, 2010

Android #7: Working with Android Intents - Part 2

Refer the previous post to get background on Intents - http://satworks.blogspot.com/2010/12/android-6-working-with-android-intents.html

Implicit Intents example: Following example demonstrates the inter-activity communication and will show on invoking some of the built-in Android activities from one activity







layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/msg" />
<Button android:id="@+id/btnDialId" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/btnDialTxt"
android:gravity="right|center_vertical" />
<Button android:id="@+id/btnCallId" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/btnCallTxt"
android:gravity="right|center_vertical" />
<Button android:id="@+id/btnSMSId" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/btnSMSTxt"
android:gravity="right|center_vertical" />
<Button android:id="@+id/btnSearchId" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/btnSearchTxt"
android:gravity="right|center_vertical" />
<Button android:id="@+id/btnBrowseId" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/btnBrowseTxt"
android:gravity="right|center_vertical" />
<Button android:id="@+id/btnGoogleMapId" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/btnGoogleMapTxt"
android:gravity="right|center_vertical" />
<Button android:id="@+id/btnViewContactsId"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="@string/btnViewContactsTxt" android:gravity="right|center_vertical" />
</LinearLayout>

ImplicitIntentsActivity.java
package in.satworks.android.samples.activity;

import android.app.Activity;
import android.app.SearchManager;
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;

public class ImplicitIntentsActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// To Dial a number
launchDialer();
// To Call a Number
// Requires android.permission.CALL_PHONE
callNumber();
// To SMS a Number
launchSMS();
// To perform local search
performLocalSearch();
// To open browser with URL
launchBrowser();
// To launch Google Maps
showGoogleMap();
// To View existing contacts stored in phone
showContacts();
}

// To View existing contacts stored in phone
private void showContacts() {
Button viewContacts = (Button) findViewById(R.id.btnViewContactsId);
viewContacts.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent contacts = new Intent();
contacts.setAction(android.content.Intent.ACTION_VIEW);
contacts.setData(ContactsContract.Contacts.CONTENT_URI);
startActivity(contacts);
}
});

}

// To launch Google Maps
private void showGoogleMap() {
Button googleMapBtn = (Button) findViewById(R.id.btnGoogleMapId);
googleMapBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri
.parse("geo:13.05,80.20"));
startActivity(intent);
}
});
}

// Open a browser window to the URL specified.
private void launchBrowser() {
Button browseBtn = (Button) findViewById(R.id.btnBrowseId);
browseBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri
.parse("http://www.android.com"));
startActivity(intent);
}
});
}

// Perform local search
private void performLocalSearch() {
Button searchBtn = (Button) findViewById(R.id.btnSearchId);
searchBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_SEARCH);
intent.putExtra(SearchManager.QUERY, "Test");
startActivity(intent);
}
});
}

// To launch SMS
private void launchSMS() {
Button smsBtn = (Button) findViewById(R.id.btnSMSId);
smsBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri
.parse("sms:123456789"));
intent.putExtra("sms_body", "Test SMS message.....");
startActivity(intent);
}
});
}

// Calls the entered phone number.
private void callNumber() {
Button callBtn = (Button) findViewById(R.id.btnCallId);
callBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_CALL, Uri
.parse("tel:123456789"));
startActivity(intent);
}
});
}

// Dials (but does not actually initiate the call) the number given
private void launchDialer() {
Button dialBtn = (Button) findViewById(R.id.btnDialId);
dialBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri
.parse("tel:123456789"));
startActivity(intent);
}
});
}
}

values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ImplicitIntentsApp</string>
<string name="msg">ImplicitIntentsActivity! \nTry out few of the predefined intents\n</string>
<string name="btnDialTxt">Launch Dialer</string>
<string name="btnCallTxt">Launch Call</string>
<string name="btnSMSTxt">Launch SMS</string>
<string name="btnSearchTxt">Launch Search</string>
<string name="btnBrowseTxt">Launch Browser</string>
<string name="btnGoogleMapTxt">Launch Google Map</string>
<string name="btnViewContactsTxt">Launch Phone Contacts</string>
</resources>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.satworks.android.samples.activity" android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ImplicitIntentsActivity"
android:label="@string/app_name">
<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" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
</manifest>

1 comment: