2015-10-05 28 views
0

在我的應用程序中撥打電話。通話結束後,我需要獲得通話時長,並從設備的通話記錄列表中刪除該通話記錄。這一切功能都能夠按照預期正常工作。現在的問題是,當我從默認設備調用的應用程序外部撥打電話時,它將刪除當前的呼叫。如何限制它? 正在使用共享偏好的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(); 
      } 

     } 
    } 
} 

回答

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(); 
+0

其工作。我也嘗試過使用未註冊的onpause接收器。但是當一個調用開始時,一個Activity onpause總是被調用。所以我認爲唯一的選擇仍然是使用標誌。 – Meenaxi

+0

太棒了!享受@梅納西:) –