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

3 に答える 3

0

番号がダイヤルされると状態が OFF_HOOk に変更されるため、間違った期間が表示されます。しかし、実際にはそうすべきではありません。相手側のユーザーが通話を受け入れると、OFF_HOOk に変更されます。

残念ながら、デバイスは android での発信通話の呼び出し状態を上げません。OFF_Hookにダイレクトに切り替わります!!

于 2013-07-26T07:33:47.593 に答える
0

このコードは Android 4 または 5 の Android Sdk で動作します:- この行から Subcrption id を取得します:- whichSIM = intent.getExtras().getInt("subscription");

見出し

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { バンドル バンドル = インテント.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. リスト項目
于 2019-08-31T10:32:07.020 に答える