Thursday 20 November 2014

Android programming - Phone Number Utility

Click button utk buat call.

Ini adalah salah satu fungsi dalam app Kad Jemputan aku. Tapi source code Kad Jemputan aku dah hilang :(. Jadi aku kena cari semula semua fungsi2 yang aku guna untuk Kad Jemputan.

Salah satu fungsi yang aku guna adalah Phone Utility.

Dari android studio, aku create New Project. Aku guna default source code yang Android Studio generate iaitu "Hello World". Menggunakan source code yang sama, aku tambah beberapa command line untuk Phone Utility. Semua command yg aku tambah di highlighted dengan warna kuning.

Moga2 ini akan menjadi rujukan aku masa depan & sapa-sapa yang memerlukan. Somoga ada manfaat.



Ada 3 file ni.

MainActivity.java

package com.hobby.azbiha.myapplication;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    Button b_Mariam, b_Maslina;
    public static final String MARIAM = "012xxxxxxx";
    public static final String MASLINA = "01xxxxxxx";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        Button b_Mariam = (Button) findViewById(R.id.btnMariam);
        Button b_Maslina = (Button) findViewById(R.id.btnMaslina);
    
        b_Mariam.setOnClickListener(this);
        b_Maslina.setOnClickListener(this);
    
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View arg1) {
        switch (arg1.getId()) {
            case R.id.btnMariam:
                Intent b1 = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + MainActivity.MARIAM));
                startActivity(b1);
                break;

            case R.id.btnMaslina:

                Intent b2 = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + MainActivity.MASLINA));
                startActivity(b2);
                break;

        }

    }
}


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.hobby.azbiha.myapplication" >

    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@style/AppTheme" >
        <uses-permission android:name="android.permission.CALL_PHONE" />
        <uses-permission android:name="android.permission.modify_phone_state" />

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

        <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
        <activity            android:name=".MainActivity"            android:label="@string/app_name"            android:theme="@style/AppTheme.NoActionBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>





activity_main.xml

<?xml version="1.0" encoding="utf-8"?><android.support.design.widget.CoordinatorLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"    android:layout_height="match_parent" android:fitsSystemWindows="true"    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton android:id="@+id/fab"        android:layout_width="wrap_content" android:layout_height="wrap_content"        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"        android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

content_main.xml


<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:paddingBottom="@dimen/activity_vertical_margin"    app:layout_behavior="@string/appbar_scrolling_view_behavior"    tools:showIn="@layout/activity_main" tools:context=".MainActivity">

    <TextView android:text="Hello World!" android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/textView" />

    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Maslina"        android:id="@+id/btnMaslina"        android:layout_below="@+id/textView"        android:layout_alignParentLeft="true"        android:layout_alignParentStart="true"        android:layout_marginTop="34dp" />

    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Mariam"        android:id="@+id/btnMariam"        android:layout_alignTop="@+id/textView"        android:layout_alignParentLeft="true"        android:layout_alignParentStart="true" />
</RelativeLayout>

OUTPUT