Saturday 24 November 2012

Implicit Intents - Opening Contacts, Opening Browser, Make a Phone call








1.ImplicitIntentsDemo2 Activity:

package com.venkool;

import java.net.URI;

public class ImplicitIntentsDemo2Activity extends Activity {
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            Button btn = (Button) findViewById(R.id.btnClick);

            btn.setOnClickListener(new OnClickListener() {

                  @Override
                  public void onClick(View v) {
                        // TODO Auto-generated method stub

                        Intent intent = new Intent();
                        intent.setAction(android.content.Intent.ACTION_VIEW);

                        intent.setData(ContactsContract.Contacts.CONTENT_URI);

                        startActivity(intent);
                  }
            });

            Button btnBrowser = (Button) findViewById(R.id.btnOpenBrowser);

            btnBrowser.setOnClickListener(new OnClickListener() {

                  @Override
                  public void onClick(View v) {
                        // TODO Auto-generated method stub

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

            Button btnPhoneCall = (Button) findViewById(R.id.btnMakingPhoneCall);
            btnPhoneCall.setOnClickListener(new OnClickListener() {

                  @Override
                  public void onClick(View v) {
                        // TODO Auto-generated method stub

                        Intent intent = new Intent(Intent.ACTION_CALL, Uri
                                    .parse("tel:1345"));

                        startActivity(intent);
                  }
            });

      }
}



2.main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/Background"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="70dp"
        android:gravity="center"
        android:text="Implicit Intents"
        android:textSize="15dp"
        android:textStyle="bold" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:text="Using Unknown Activities"
        android:textSize="20dp" />

    <Button
        android:id="@+id/btnClick"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="Opening Contacts on Mobile" />

    <Button
        android:id="@+id/btnOpenBrowser"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:text="Opening Browser"
        android:textSize="15dp" />

    <Button
        android:id="@+id/btnMakingPhoneCall"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:text="Phone Call" />


</LinearLayout>



3.Colors adding Strings.xml:

    <color name="Background">#7B68EE</color>



4.Add permission on manifest.xml:

    <uses-permission android:name="android.permission.CALL_PHONE"/>
    <uses-permission android:name="android.permission.INTERNET"/>

Implicit Intents - Same Action and Category tags to apply two Activities

Demo:



1.ImplicitIntentsDemo1 Activity:

package com.venkool;

import android.app.Activity;

public class ImplicitIntentsDemo1Activity extends Activity {
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.first);

            Button btnSecond = (Button) findViewById(R.id.btnSecond);

            btnSecond.setOnClickListener(new OnClickListener() {

                  @Override
                  public void onClick(View v) {
                        // TODO Auto-generated method stub

                        Intent intent = new Intent();
                        intent.setAction("action2");
                        intent.addCategory("category2");
                        startActivity(intent);
                  }
            });

            Button btnThird = (Button) findViewById(R.id.btnThird);

            btnThird.setOnClickListener(new OnClickListener() {

                  @Override
                  public void onClick(View v) {
                        // TODO Auto-generated method stub

                        Intent intent = new Intent();
                        intent.setAction("action2");
                        intent.setAction("action3");
                        intent.addCategory("category2");
                        intent.addCategory("category2");
                        startActivity(intent);
                  }
            });
      }
}


2.SecondScreen Activity:


package com.venkool;

import android.app.Activity;

public class SecondScreen extends Activity {
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.second);

      }
}


3.Third Screen Activity:


package com.venkool;

import android.app.Activity;

public class ThirdScreen extends Activity {
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.third);

      }
}


4.first.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/FirstBackground"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:text="Implicit Intents Demo"
        android:textSize="20dp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/btnSecond"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:gravity="center"
        android:text="Second" />

    <Button
        android:id="@+id/btnThird"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:gravity="center"
        android:text="Third" />

</LinearLayout>



5.second.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/SecondBackground"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="70dp"
        android:gravity="center"
        android:text="Second Screen"
        android:textSize="30dp"
        android:textStyle="bold" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:text="Implicit Intents"
        android:textSize="20dp"
        android:textStyle="bold" />

</LinearLayout>



6.third.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/ThirdBackground"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="70dp"
        android:gravity="center"
        android:text="Third Screen"
        android:textSize="30dp"
        android:textStyle="bold" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:text="Implicit Intents"
        android:textSize="20dp"
        android:textStyle="bold" />

</LinearLayout>



7.Colors adding Strings.xml:

    <color name="FirstBackground">#556B2F</color>
    <color name="SecondBackground">#9932CC</color>
    <color name="ThirdBackground">#4B0082</color>



8.Add activity to manifest file:

<activity
            android:label="Second Screen"
            android:name=".SecondScreen" >
            <intent-filter >
                <action android:name="action2" />
                <action android:name="action3" />

                <category android:name="category2" />
                <category android:name="category3" />

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:label="Third Screen"
            android:name=".ThirdScreen" >
            <intent-filter >
                <action android:name="action2" />
                <action android:name="action3" />

                <category android:name="category2" />
                <category android:name="category3" />

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
</activity>