仅在从app调用make时删除通话记录

时间:2015-10-05 06:49:13

标签: android broadcastreceiver calllog

在我的应用程序中正在拨打电话。通话结束后,我需要获得通话时间并从设备的通话记录列表中删除该通话条目。这一切功能都按预期正常工作。现在的问题是,当我从应用程序外部拨打电话时,如默认设备呼叫,它会删除当前呼叫。如何限制它? 我正在使用BroadcastReceiver,如下所示

 @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();
            }

        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以获取共享首选项的帮助,以存储任何标记,表明您正在通过应用程序拨打电话的天气。 例如:

当你从你的应用程序设置共享首选项时,这样;

SharedPreferences sharedPreferences = getSharedPreferences("AppCallingFlag",
            Context.MODE_PRIVATE);
 SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString("app_flag", 1);
    editor.commit();

并在广播接收器中检查此标志,如下所示:

SharedPreferences sharedPreferences = context.getSharedPreferences("AppCallingFlag",
            Context.MODE_PRIVATE);
         int appFlag =  sharedPreferences.getInt("app_flag",-1);
    if(appFlag == 1){
         //delete logs
    }esle{
       //do nothing
    }

并且在所有操作之后将共享首选项中的app_flag设置为0,如下所示:

SharedPreferences sharedPreferences = getSharedPreferences("AppCallingFlag",
            Context.MODE_PRIVATE);
 SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString("app_flag", 0);
    editor.commit();