Monday, 6 January 2014

Android Asynctask… is this the correct way to do it?


CounterTask.java:
package my.tasks;

import java.util.Random;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Log;

/**
 * @author 2
 * 
 *         Simulates a time consuming task, counts in the background from 0 to
 *         the given value in first parameter. Its progress is published via an
 *         integer which is a percentual value 0..100. The task may not
 *         complete, it may fail or it may be cancelled by the user. In case of
 *         success it returns true.
 */
public class CounterTask extends AsyncTask<Integer, Integer, Boolean> {

private static final long DELAY = 100;
private static final int PROBABILITY_TO_FAIL_EVERY_STEP = 1;
private static final String TAG = "CounterTask";
private ProgressDialog progressDialog;
private Random random;

/**
 * @param progressDialog
 *            a ProgressDialog the task can use to display its progress.
 */
public CounterTask(ProgressDialog progressDialog) {
    this.progressDialog = progressDialog;
    this.progressDialog.setMax(100);
    this.random = new Random();
}

/*
 * (non-Javadoc)
 * 
 * @see android.os.AsyncTask#doInBackground(Params[])
 * 
 * Set params[0] to the maximum value you want the task to count to.
 */
@Override
protected Boolean doInBackground(Integer... params) {
    validateParams(params);
    int count_max = params[0];

    for (int i = 0; i < count_max; i++) {
        int progressPercent = calculateProgressPercent(i, count_max);
        publishProgress(progressPercent);
        Log.i(TAG, "Counter: " + Integer.toString(i));
        Log.i(TAG, "Progress published: " + Integer.toString(progressPercent) + "%");
        if (sleep() == false) {
            return false;
        }
        if (doesFail(PROBABILITY_TO_FAIL_EVERY_STEP)) {
            Log.i(TAG, "Background fails.");
            return false;
        }
    }

    Log.i(TAG, "Background succeeds.");
    return true;
}

@Override
protected void onCancelled() {
    super.onCancelled();
    Log.i(TAG, "Background cancelled.");
}

@Override
protected void onPostExecute(Boolean result) {
    super.onPostExecute(result);
    progressDialog.dismiss();

    String message = null;
    if (result)
        message = "Background succeeded.";
    else
        message = "Background failed.";
    Log.i(TAG, message);
    createAlertDialog(message).show();
}

private Dialog createAlertDialog(String message) {
    AlertDialog.Builder builder = new Builder(progressDialog.getContext());
    AlertDialog dialog = builder.setMessage(message).setTitle("Result").setCancelable(true).create();
    return dialog;
}

@Override
protected void onProgressUpdate(Integer... values) {
    super.onProgressUpdate(values);
    progressDialog.setProgress(values[0]);
}

/**
 * Helper method to decide if the task fails in this step.
 * 
 * @param probabilityToFail
 *            Value 0..100 the probability to fail.
 * @return true if it fails, false if not
 */
private boolean doesFail(int probabilityToFail) {

    int prob = random.nextInt(100);
    if (prob < probabilityToFail)
        return true;
    else
        return false;
}

/**
 * The thread sleeps from step to step to simulate a time consuming task.
 * 
 * @return
 */
private boolean sleep() {
    try {
        Thread.sleep(DELAY);
    } catch (InterruptedException e) {
        return false;
    }
    return true;
}

private int calculateProgressPercent(int done, int max) {
    done = done * 100;
    return done / max;
}

private void validateParams(Integer... integers) {
    if (integers == null || integers.length != 1 || integers[0] < 0)
        throw new IllegalArgumentException();
}
}





MainActivity.java:
package my.application;

import my.tasks.CounterTask;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener, OnCancelListener {

private CounterTask counterTask;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button startButton = (Button) findViewById(R.id.startButton);
    startButton.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    int id = view.getId();

    switch (id) {
    case R.id.startButton:
        ProgressDialog progressDialog = createProgressDialog();
        progressDialog.show();
        CounterTask ct = new CounterTask(progressDialog);
        counterTask = (CounterTask) ct.execute(100);
        break;

    default:
        break;
    }
}

private ProgressDialog createProgressDialog() {

    ProgressDialog progressDialog = new ProgressDialog(this);
    progressDialog.setProgress(0);
    progressDialog.setMax(100);
    progressDialog.setTitle("Progress");
    progressDialog.setCancelable(true);
    progressDialog.setOnCancelListener(this);
    progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    return progressDialog;

}

@Override
public void onCancel(DialogInterface arg0) {
    counterTask.cancel(true);
}

}

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;
           
            }