在Android中使用广播接收器检测呼入和呼出电话

3

我一直面临着这个问题,而且一直没有能够解决以下问题:

我正在使用广播接收器来检测呼入和呼出通话的详细信息。

现在问题出在通话持续时间上。对于呼入电话,它显示的持续时间是正确的,但对于呼出电话,它却显示了错误的持续时间...

请帮助我解决这个问题。提前感谢您花费宝贵的时间帮我解决这个问题。

以下是我的代码:

public class IncomingCallReceiver extends BroadcastReceiver {
// db instance variables
DBAdapter dba;

// instance variables of sharedpreferences
SharedPreferences mSharedPrefernce;
Editor e;

// String variables for number,date,time,calltype
String number, date, time, calltype;
long startTime, endTime;

@Override
public void onReceive(Context context, Intent intent) {
    Log.v("info", "calls info....");

    // initialising the sharedpreferences
    mSharedPrefernce = context.getSharedPreferences("MyPref", 0);
    e = mSharedPrefernce.edit();

    // Creating object for the DBAdapter
    dba = new DBAdapter(context);
    Bundle bundle = intent.getExtras();

    // Log.v("info", bundle.toString());
    if (bundle == null)
        return;

    // initialising the variables
    number = null;
    startTime = 0;
    endTime = 0;

    // getting incoming call details
    String state = bundle.getString(TelephonyManager.EXTRA_STATE);
    if ((state != null)
            && (state
                    .equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))) {

        Log.v("info", "Phone Ringing..");

        number = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
        Log.v("info", "Incomng Number: " + number);

        calltype = "Incoming";

        Time today = new Time(Time.getCurrentTimezone());
        today.setToNow();

        date = today.monthDay + "-" + (today.month + 1) + "-" + today.year;
        time = today.format("%k:%M:%S");

        // putting the values into the SharedPreferences
        e.putString("number", number);
        e.putString("Type", calltype);
        e.putString("date", date);
        e.putString("time", time);
        e.commit();

        Toast.makeText(
                context,
                "Detect Calls sample application\nIncoming number: "
                        + number, Toast.LENGTH_SHORT).show();

    }
    // getting outgoing call details
    else if (state == null) {
        number = bundle.getString(Intent.EXTRA_PHONE_NUMBER);
        Log.v("info", "Outgoing Number: " + number);

        calltype = "Outgoing";

        Time today = new Time(Time.getCurrentTimezone());
        today.setToNow();

        date = today.monthDay + "-" + (today.month + 1) + "-" + today.year;
        time = today.format("%k:%M:%S");

        // putting the values into the SharedPreferences
        e.putString("number", number);
        e.putString("Type", calltype);
        e.putString("date", date);
        e.putString("time", time);
        e.commit();

        Toast.makeText(
                context,
                "Detect Calls sample application\nOutgoing number: "
                        + number, Toast.LENGTH_SHORT).show();

    }
    // called when the call is answered
    else if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
        Log.v("info", "Call Ansered..");

        startTime = System.currentTimeMillis();
        e.putLong("start", startTime);
        e.commit();

    } 
    // called when the call is ended
    else if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_IDLE)) {
        Log.v("info", "Call Ended..");

        String phonenumber=null, type=null, date1=null, time1=null,  duration=null;

        // getting the values from the SharedPreferences
        phonenumber = mSharedPrefernce.getString("number", "");
        type = mSharedPrefernce.getString("Type", "");
        date1 = mSharedPrefernce.getString("date", "");
        time1 = mSharedPrefernce.getString("time", "");
        long start=0;
        start = mSharedPrefernce.getLong("start", 0);
        Log.v("info", "startTime=" + start);

        // clearing the SharedPreferences
        mSharedPrefernce.edit().clear();

        endTime = System.currentTimeMillis();
        Log.v("info", "endTime=" + endTime);
        long totalTime =0;
        totalTime = endTime - start;

        DateFormat df = new SimpleDateFormat("HH':'mm':'ss");
        df.setTimeZone(TimeZone.getTimeZone("GMT+0"));

        duration = df.format(new Date(totalTime));

        // inserting the call details into sqlite db
        dba.insertDetails(phonenumber, date1, time1, duration, type);


    }

}
     }

我也在清单文件中添加了如下所示的权限...
<receiver android:name="IncomingCallReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" >
            </action>
        </intent-filter>
    </receiver>

你找到任何解决方案了吗? - Mehul Joisar
3个回答

1
这将使您的入站号码无论是双卡还是单卡都能使用:
Bundle bundle = intent.getExtras();
String state = bundle.getString(TelephonyManager.EXTRA_STATE);
if (state != null){
    callFromSecondSimNo = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
}

0

它显示了错误的持续时间,因为一旦号码被拨打,状态就会改变为OFF_HOOK。但实际上不应该这样。它应该在对方用户接受您的呼叫后才切换到OFF_HOOK。

不幸的是,在Android中,设备不会为外向呼叫打开响铃状态!它直接切换到OFF_Hook!


如何在警告对话框中获取呼出号码..?? - Aditya
这是我的http://stackoverflow.com/questions/30635005/how-to-get-alert-dialog-only-after-disconnect-call/30635666?noredirect=1#comment49379335_30635666。 - Aditya

0
这段代码适用于 Android 4 或 5 的 Android Sdk: 你可以从这行代码中获取订阅 ID:whichSIM = intent.getExtras().getInt("subscription");
标题
如果 (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Bundle bundle = intent.getExtras();
        if (bundle != null) {
            Set<String> keys = bundle.keySet();
            Iterator<String> it = keys.iterator();
            while (it.hasNext()) {
                String key = it.next();
                Log.e("BUNDEL-VALUE","[" + key + "=" + bundle.get(key)+"]");
            }
        }
        if (intent.getExtras().containsKey("subscription")) {
            whichSIM = intent.getExtras().getInt("subscription");
            HelperFunctions.theLogger("WhichSim", "before value: " + whichSIM);
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putLong("ChooseSim", whichSIM);
            editor.commit();
        }
    }

    //Working code For 4.4 Phones KitKat
    else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        whichSIM = intent.getExtras().getInt("subscription");
        if (whichSIM == 0) {
            whichSIM = 1;
            editor.putLong("ChooseSim", whichSIM);
            editor.commit();
            // Incoming call for SIM1
            // Toast.makeText(context,"Getting Sim Id  "+whichSIM,Toast.LENGTH_LONG).show();
        } else if (whichSIM == 1) {
            whichSIM = 2;
            editor.putLong("ChooseSim", whichSIM);
            editor.commit();
            // Toast.makeText(context,"Getting Sim Id  "+whichSIM,Toast.LENGTH_LONG).show();

        }
    }
  1. 列表项

在这段代码中,您将在Bundle上获取State参数。State有3种类型,1是“RINGING”,2是“OFFHOOK”,3是“IDLE”。 我们在RINGING状态下恢复所有呼叫,在OFFHOOK上处理所有外拨呼叫,并在呼叫断开连接时使用IDLE状态进行工作。 - Rajeev Thakur

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接