In this tutorial we will show how to call an In built activity using Intents :
One of the important aspects of Android programming is using the intent service to call activities from other applications.
File : res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/btn_makecalls"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="57dp"
android:text="Make Calls" />
<Button
android:id="@+id/btn_webbrowser"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Web Browser" />
<Button
android:id="@+id/btn_chooseContact"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/btn_makecalls"
android:layout_marginTop="31dp"
android:text="Contacts" />
<Button
android:id="@+id/btn_showMap"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/btn_webbrowser"
android:layout_marginTop="34dp"
android:text="Show Map" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="Calling In Built Activities" />
</RelativeLayout>
File : MainActivity.java
package com.example.inbuilt_intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
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;
int request_Code = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.btn_webbrowser);
//---Web browser button---
b1.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0){
Intent i = new
Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://www.google.com"));
startActivity(i);
}
});
//---Make calls button---
b2 = (Button) findViewById(R.id.btn_makecalls);
b2.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0){
Intent i = new
Intent(android.content.Intent.ACTION_DIAL,
Uri.parse("tel:+64554445"));
startActivity(i);
}
});
//---Show Map button---
b3 = (Button) findViewById(R.id.btn_showMap);
b3.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0){
Intent i = new
Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("geo:8.491172,76.943897"));
startActivity(i);
}
});
//---Choose Contact button---
b4 = (Button) findViewById(R.id.btn_chooseContact);
b4.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0){
Intent i = new
Intent(android.content.Intent.ACTION_PICK);
i.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(i,request_Code);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == request_Code)
{
if (resultCode == RESULT_OK)
{
Toast.makeText(this,data.getData().toString(),
Toast.LENGTH_SHORT).show();
Intent i = new Intent(
android.content.Intent.ACTION_VIEW,
Uri.parse(data.getData().toString()));
startActivity(i);
}
}
}
}
output
O
output:
out
0 comments:
Post a Comment