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;
}

}

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

4

2 回答 2

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();
}
}
于 2013-10-25T04:25:48.233 回答
0

在您的 Callstatebroadcaster 的 onreceive 方法中添加此代码,它会正常工作。我刚刚解决了它。

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

这会奏效。

于 2016-07-28T11:57:05.520 回答