In my app am making a call. After call ends i need to get call duration and remove that call entry from call log list of device.This all functionality is working properly as expected. Now the issue is when i make a call from outside the app like from default device call it deletes the current call. how to restrict it?
am using a BroadcastReceiver
as below
@Override
public void onReceive(Context ctx, Intent intent)
{
String action = intent.getAction();
db=new DatabaseMethods(ctx);
if (action.equalsIgnoreCase("android.intent.action.PHONE_STATE"))
{
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING))
{
//when call arrives
}
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
{
Toast.makeText(ctx, "EXTRA_STATE_OFFHOOK",Toast.LENGTH_LONG).show();
start_time = System.currentTimeMillis();
AppConstants.START_TIME=Utils.formatToDateTime(start_time);
AppConstants.CALL_DURATION="00:00:00";
System.out.println("Start Time Millis"+"="+start_time);
System.out.println("Start Time"+"="+AppConstants.START_TIME);
}
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE))
{
try
{
int idOfRowToDelete = 0 ;
Uri allCalls = Uri.parse("content://call_log/calls");
String lastMinute = String.valueOf(new Date().getTime() - start_time);
//before the call started
Cursor c = ctx.getContentResolver().query(allCalls, null, Calls.DATE + " > "
+ lastMinute, null, Calls.DATE + " desc");
c.moveToFirst();
if (c.getCount() > 0) {
duration= Integer.parseInt(c.getString(c.getColumnIndex(Calls.DURATION)));
idOfRowToDelete = c.getInt(c.getColumnIndex(Calls._ID));
}
if(duration>0)
{
AppConstants.CALL_DURATION=Utils.formatSceondstoHHMMSS(duration);
}
else
{
AppConstants.CALL_DURATION="00:00:00";
}
ctx.getContentResolver().delete(allCalls, Calls._ID + "= ? ", new String[] { String.valueOf(idOfRowToDelete) });
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
db.new AsyncSaveCallDetails().execute();
}
}
}
}
You can take help of shared preferences to store any flag which indicates you weather you are making call from your app or not. For example:
When you call from your app set shared preferences like this;
SharedPreferences sharedPreferences = getSharedPreferences("AppCallingFlag",
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("app_flag", 1);
editor.commit();
and check in the broadcast receiver for this flag like this:
SharedPreferences sharedPreferences = context.getSharedPreferences("AppCallingFlag",
Context.MODE_PRIVATE);
int appFlag = sharedPreferences.getInt("app_flag",-1);
if(appFlag == 1){
//delete logs
}esle{
//do nothing
}
and after all the operations set app_flag in shared preferences to 0 like this:
SharedPreferences sharedPreferences = getSharedPreferences("AppCallingFlag",
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("app_flag", 0);
editor.commit();