Sunday, 22 December 2013

Incoming Call Broadcast Reciever


Learn Android Development


In previous I discussed about how to handle incoming call or how to listen for Incoming call using Activity, In this post I will discuss the same using Broadcast Receiver.

Broadcast Receiver is  better way than Activity to listen for Incoming Calls.

So declare the Permission and Receiver in Manifest

Permission Required:

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

Declare the receiver and register it to listen "android.intent.action.PHONE_STATE"  action

android.manifest 


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.incomingcallreciever"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />
    
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        
        
        <receiver android:name=".IncommingCallReceiver" android:enabled="true">
            <intent-filter>
                                     <action android:name="android.intent.action.PHONE_STATE" /> 
                </intent-filter>
           </receiver>

    </application>

</manifest>







Incoming Call Receiver




import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.widget.Toast;


 public class IncommingCallReceiver extends BroadcastReceiver
 {
    
      Context mContext;
      
      
      @Override
      public void onReceive(Context mContext, Intent intent)
      {
          try
          {
           
              String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

           
            
              if(state.equals(TelephonyManager.EXTRA_STATE_RINGING))
              {
                   Toast.makeText(mContext, "Phone Is Ringing", Toast.LENGTH_LONG).show();
                   // Your Code
              }
              
              if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
              {
                   Toast.makeText(mContext, "Call Recieved", Toast.LENGTH_LONG).show();
                       // Your Code
              }
              
              if (state.equals(TelephonyManager.EXTRA_STATE_IDLE))
              {
                
                  Toast.makeText(mContext, "Phone Is Idle", Toast.LENGTH_LONG).show();
                      // Your Code
                
              }
          }
          catch(Exception e)
          {
              //your custom message
          }
       
     }
      
}






      

Getting IMEI Number and other Details

IMEI Nmber Using TelePhony Manager

In Android, using TelephonyManager we can get many details of the phone and SIM like IMEI Number, SIM Serial ID, SIM state, Roaming information etc.




  



In order to get these details we must declare following permission in AndroidManifest file..
<uses-permission android:name="android.permission.READ_PHONE_STATE" />



Have an  object of TelephonyMnager
TelephonyManager  tm=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

Get IMEI Number of Phone

         String IMEINumber=tm.getDeviceId();

 Get Subscriber ID

          String subscriberID=tm.getDeviceId();

  Get SIM Serial Number

            String SIMSerialNumber=tm.getSimSerialNumber();

 Get Network Country ISO Code

             String networkCountryISO=tm.getNetworkCountryIso();

 Get SIM Country ISO Code

               String SIMCountryISO=tm.getSimCountryIso();

 Get the device software version

               String softwareVersion=tm.getDeviceSoftwareVersion()

 Get the Voice mail number

               String voiceMailNumber=tm.getVoiceMailNumber();


 Get the Phone Type CDMA/GSM/NONE

            int phoneType=tm.getPhoneType();

            switch (phoneType)
            {
                    case (TelephonyManager.PHONE_TYPE_CDMA):

                               // your code
                                   break;
                    case (TelephonyManager.PHONE_TYPE_GSM) 

                               // your code                 
                                   break;
                    case (TelephonyManager.PHONE_TYPE_NONE):

                               // your code              
                                    break;
             }


 Find whether the Phone is in Roaming, returns true if in roaming

             boolean isRoaming=tm.isNetworkRoaming();
              if(isRoaming)
                      phoneDetails+="\nIs In Roaming : "+"YES";
              else
                     phoneDetails+="\nIs In Roaming : "+"NO";


Get the SIM state

            int SIMState=tm.getSimState();
            switch(SIMState)
            {
                    case TelephonyManager.SIM_STATE_ABSENT :
                        // your code
                        break;
                    case TelephonyManager.SIM_STATE_NETWORK_LOCKED :
                        // your code
                        break;
                    case TelephonyManager.SIM_STATE_PIN_REQUIRED :
                        // your code
                        break;
                    case TelephonyManager.SIM_STATE_PUK_REQUIRED :
                        // your code
                        break;
                    case TelephonyManager.SIM_STATE_READY :
                        // your code
                        break;
                    case TelephonyManager.SIM_STATE_UNKNOWN :
                        // your code
                        break;
           
            }

Tuesday, 10 December 2013

Problem with handling event on radio buttons in Android programming

XML-Code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <RadioGroup android:id="@+id/que_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <RadioButton android:layout_height="wrap_content"
            android:layout_width="wrap_content" android:id="@+id/default_mode"
            android:text="Default Mode" android:checked="true"></RadioButton>
        <RadioButton android:layout_height="wrap_content"
            android:layout_width="wrap_content" android:id="@+id/warn_mode"
            android:text="Warn Mode"></RadioButton>
        <RadioButton android:layout_height="wrap_content"
            android:layout_width="wrap_content" android:id="@+id/grey_mode"
            android:text="Grey Mode"></RadioButton>
    </RadioGroup>
</LinearLayout>  
Java -code
package com.test.radiogrouptest;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class RadioGroupTestActivity extends Activity {
    public static final String TAG = "RGTA";
    RadioGroup queRG;
    RadioButton defaultModeRB, warnModeRB, greyModeRB;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        queRG = (RadioGroup) findViewById(R.id.que_group);
        defaultModeRB = (RadioButton) findViewById(R.id.default_mode);
        warnModeRB = (RadioButton) findViewById(R.id.warn_mode);
        greyModeRB = (RadioButton) findViewById(R.id.grey_mode);
        queRG.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            public void onCheckedChanged(RadioGroup rg, int checkedId) {
                if (defaultModeRB.getId() == checkedId) {
                    defaultMethod();
                    colorTouched();
                    return;
                } else if (warnModeRB.getId() == checkedId) {
                    warnMethod();
                    return;
                } else if (greyModeRB.getId() == checkedId) {
                    greyMethod();
                    return;
                }
            }
        });
    }

    public void defaultMethod() {
        Log.d("TAG", "defaultMethod");
    }

    public void colorTouched() {
        Log.d("TAG", "colorTouched");
    }

    public void warnMethod() {
        Log.d("TAG", "warnMethod");
    }

    public void greyMethod() {
        Log.d("TAG", "greyMethod");
    }
}
 
 
 
note: if u  having problem in click event  add RadioGroup ,here i mentioned blue clor