Wednesday, 16 January 2013

Implicit Intent

 Implicit Intents do not specify the java class which should be called .
They specify the action which should be performed and optionally an URI which should be used for this action.
For example following tell the android system to view a webpage.
Intent intent = new Intent(Intent.ACTION_VIEW,URI.Parse("http://www.google.com"));

ScreenShots:



activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/googleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="152dp"
        android:text="Google" />

</RelativeLayout>

MainActivity.java:
package com.ram.implicitintent;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

    Button gbtn= (Button)findViewById(R.id.googleButton);

gbtn.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

Intent webIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(webIntent);

}

});
}

 
}