2013-10-25 38 views
0

嗨我在这个论坛搜索了很多关于正确的结果,但无法找到。一旦通话结束,我需要上一次去电的详细信息。为此,我现在用的BroadcasteReceiver这里是代码为我的接收机通话结束后的最后一个去电详细信息

public class CallStateBroadcaster extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 
    // TODO Auto-generated method stub 


    ((TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE)).listen(new CustomPhoneStateListener(context), PhoneStateListener.LISTEN_CALL_STATE); 

}} 

这里是PhoneStateListener

代码
public class CustomPhoneStateListener extends PhoneStateListener{ 

private Context context; 

    public CustomPhoneStateListener(Context paramContext) 
    { 
    this.context = paramContext; 
    } 

@Override 
public void onCallStateChanged(int state, String incomingNumber) { 
    // TODO Auto-generated method stub 
    super.onCallStateChanged(state, incomingNumber); 
    ((TelephonyManager)this.context.getSystemService("phone")).listen(this, PhoneStateListener.LISTEN_NONE); 
    if(TelephonyManager.CALL_STATE_IDLE == state) 
    { 
    try { 
     Thread.sleep(1500L); 
     Intent intent = new Intent(this.context,LastCallInfoActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); 
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     this.context.startActivity(intent); 

    } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    } 
} 

这里是我的活动代码获取通话细节的通话记录

public class LastCallInfoActivity extends Activity{ 

String addtolist; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 

    Cursor callDetailCursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null,null,null,android.provider.CallLog.Calls.DATE + " DESC limit 1"); 
    int phoneNumber= callDetailCursor.getColumnIndex(CallLog.Calls.NUMBER); 
    int callType=callDetailCursor.getColumnIndex(CallLog.Calls.TYPE); 
    int callDate=callDetailCursor.getColumnIndex(CallLog.Calls.DATE); 
    int callDuration=callDetailCursor.getColumnIndex(CallLog.Calls.DURATION); 


    Log.i(">>CAllDetails", "getsCallLogs"); 

    if(callDetailCursor.getCount()>0) 
    { 
     while(callDetailCursor.moveToNext()) 
     { 
      String phoneNumberString=callDetailCursor.getString(phoneNumber); 
      String contactName= getContactName(this, phoneNumberString); 
      String callTypeString =callDetailCursor.getString(callType); 
      String callDateString=callDetailCursor.getString(callDate); 
      String callDurationString=callDetailCursor.getString(callDuration); 
      Date callDayTime=new Date(Long.valueOf(callDateString)); 

      int callCode = Integer.parseInt(callTypeString); 
      int calldur=Integer.parseInt(callDurationString); 
      if (callCode==2 && calldur>=1) 
      { 
       Double callCost=Double.parseDouble(callDurationString); 


       String callCostString= String.valueOf(callCost); 
       Log.i(">>CAllDetails", "getsLocation"); 


       addtolist= "Name :"+contactName+"\n"+ 
         "Phone Number: "+phoneNumberString+"\n"+"Call Duration :"+ 
         callDurationString+" Seconds\n"+"Call Date: "+callDayTime+"\n"+ 
       callCostString; 



      } 
     } 
    }callDetailCursor.close(); 

    Toast.makeText(this, addtolist, Toast.LENGTH_LONG).show(); 
} 


public String getContactName(Context context, String phoneNumber) { 
    ContentResolver cr = context.getContentResolver(); 
    Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber)); 
    Cursor cursor = cr.query(uri, new String[]{PhoneLookup.DISPLAY_NAME}, null, null, null); 
    if (cursor == null) { 
     return null; 
    } 
    String contactName = null; 
    if(cursor.moveToFirst()) { 
     contactName = cursor.getString(cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME)); 
    } 

    if(cursor != null && !cursor.isClosed()) { 
     cursor.close(); 
    } 

    return contactName; 
} 

} 

我的这个节目甚至在我的电话断开之前就显示敬酒,并开始我的活动。请帮助我,并更正我的代码,以便在通话结束后执行 谢谢

回答

0

只需尝试添加一个额外的if语句以检查以前的状态是否摘机。

boolean wasoffhook=false; 

    if(TelephonyManager.CALL_STATE_OFFHOOK == state) 

    { 
     wasoffhook=true; 
    } 
    if(TelephonyManager.CALL_STATE_IDLE == state&&wasoffhook) 
    { 
    try { 
    Thread.sleep(1500L); 
    Intent intent = new Intent(this.context,LastCallInfoActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    this.context.startActivity(intent); 

} catch (InterruptedException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
} 
+0

您的解决方案无法正常工作。它不会进入if(TelephonyManager.CALL_STATE_OFFHOOK ==状态) { wasoffhook = true; } – TechHelp

0

在您的Callstatebroadcaster接收方法中添加此代码,它将工作正常。我刚解决它。

@Override 
public void onReceive(Context context, Intent intent) 
{ 

    try 
    { 
     if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) 
     { 
      savedNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER"); 
      Toast.makeText(context,"the no. is"+savedNumber,Toast.LENGTH_LONG).show(); 
      // onOutgoingCallStarted(context,"",callStartTime); 

     } 
     else 
     { 
      String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE); 
      String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER); 
      int state = 0; 

      if(stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)) { 
       switch (state) { 
        case TelephonyManager.CALL_STATE_IDLE: 
         Log.d("Testing", "Outgoing call has been disconnect"); 
         intent = new Intent(context, MyCustomDialog.class); 
         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
         intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
         context.startActivity(intent); 
         System.out.println("CAll has been disconnect..............."); 
     //       Toast.makeText(this, "CAll has been disconnect", Toast.LENGTH_LONG).show(); 
         break; 
       } 
      } 
      else if(stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) 
      { 
       state = TelephonyManager.CALL_STATE_OFFHOOK; 
      } 
      else if(stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)) 
      { 
       state = TelephonyManager.CALL_STATE_RINGING; 
      } 

      onCallStateChanged(context, state, number); 
     } 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
} 

并在Callstatechanged方法中添加此代码。

public void onCallStateChanged(Context context, int state, String number) 
{ 
    if(lastState == state) 
    { 
     //No change, debounce extras 
     return; 
    } 
    switch (state) 
    { 
     case TelephonyManager.CALL_STATE_RINGING: 
      isIncoming = true; 
      callStartTime = new Date(); 
      savedNumber = number; 
      // onIncomingCallStarted(context, number, callStartTime); 
      // onIncomingCallEnded(context, savedNumber, callStartTime, new Date()); 
      // onOutgoingCallEnded(context, savedNumber, callStartTime, new Date()); 
      // wasRinging = true; 
      break; 

     case TelephonyManager.CALL_STATE_OFFHOOK: 

      if (!wasRinging) { 
       // Start your new activity 
       onIncomingCallEnded(context,savedNumber,callStartTime,new Date()); 
       onOutgoingCallEnded(context,savedNumber,callStartTime,new Date()); 
      } else { 
       onIncomingCallStarted(context,savedNumber,callStartTime); 
       // Cancel your old activity 
      } 

      // this should be the last piece of code before the break 

      break; 


     case TelephonyManager.CALL_STATE_IDLE: 
      if(isIncoming) 
      { 
       onIncomingCallEnded(context, savedNumber, callStartTime, new Date()); 
      } 
    } 
    lastState = state; 
} 

这会奏效。

相关问题