android - 使用 broadcastReceiver 检测 android 中的来电和去电

标签 android

我长期以来一直面临这个问题,但无法解决以下问题.. 我正在使用广播接收器来检测来电和去电的详细信息。 现在是通话时长的问题。对于来电,它显示正确的持续时间,但对于去电,它显示错误的持续时间...

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

这是我的代码..

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


    }

}
     }

我还像这样在 list 文件中添加了权限..

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

最佳答案

无论是双卡还是单卡,这都会给你传入的号码:

Bundle bundle = intent.getExtras();
String state = bundle.getString(TelephonyManager.EXTRA_STATE);
if (state != null){
    callFromSecondSimNo = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
}

关于android - 使用 broadcastReceiver 检测 android 中的来电和去电,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14210554/

相关文章:

android - 如何禁用底部栏中已选择的项目

android - 使用本地分支编译 React Native 项目 - NDK_PROJECT_PATH

java - 获取自定义列表适配器以正确膨胀多个线性布局

android - 在复制/剪切选择上覆盖或隐藏工具栏

java - 如何从 View 构造函数设置 layout_width/height

android - ICS 中的 SimpleCursorAdapter

安卓工作室 3.0 : buildToolsVersion not found in gradle files

android - ViewPager 加载空白屏幕,直到用户单击屏幕上的任意位置

安卓.view.InflateException : Binary XML file line #9: Error inflating class fragment

Android - 在哪里设置按下,聚焦......选项卡的颜色?