仅在从应用程序拨打电话时删除通话记录
Remove call log only when call make from app
正在我的应用程序中打电话。通话结束后,我需要获取通话时长并从 device.This 的通话记录列表中删除该通话条目,所有功能都按预期正常工作。现在的问题是,当我从应用程序外部拨打电话时,例如从默认设备拨打电话时,它会删除当前通话。怎么限制呢?
我正在使用 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();
}
}
}
}
您可以借助共享首选项来存储任何标志,这些标志表明您是否天气是否正在从您的应用程序拨打电话。
例如:
当您从您的应用呼叫时,像这样设置共享偏好;
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();
正在我的应用程序中打电话。通话结束后,我需要获取通话时长并从 device.This 的通话记录列表中删除该通话条目,所有功能都按预期正常工作。现在的问题是,当我从应用程序外部拨打电话时,例如从默认设备拨打电话时,它会删除当前通话。怎么限制呢?
我正在使用 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();
}
}
}
}
您可以借助共享首选项来存储任何标志,这些标志表明您是否天气是否正在从您的应用程序拨打电话。 例如:
当您从您的应用呼叫时,像这样设置共享偏好;
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();