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"/>

No comments:

Post a Comment