我该如何模拟CallStateChange

时间:2014-05-14 11:24:54

标签: android telephonymanager phone-state-listener

PhoneStateListener onCallStateChanged。 我尝试了几个动作,以便调用onCallStateChanged

AudioManager am = (AudioManager) ctx.getSystemService(Context.AUDIO_SERVICE);
am.setMode(AudioManager.MODE_IN_CALL);

Intent intent = new Intent();
intent.setAction("android.intent.action.PHONE_STATE");
intent.putExtra(TelephonyManager.EXTRA_STATE, TelephonyManager.EXTRA_STATE_OFFHOOK);
sendBroadcast(intent);

但两种方式都没有达到预期的效果。 有没有办法(以编程方式)我可以做onCallStateChanged被调用?

1 个答案:

答案 0 :(得分:0)

在切换按钮上尝试,

tb.setOnClickListener(new ToggleButton.OnClickListener() {

        public void onClick(View v) {
            SharedPreferences sharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(getApplicationContext());
            Editor editor = sharedPreferences.edit();
            editor.putBoolean("main_state", tb.isChecked());
            editor.commit();
        }
    });

也可以调用onCallStateChanged方法,

public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
    if (arg1) {
        edit.putBoolean("main_state", arg1);


    } else {
        edit.putBoolean("main_state", arg1);

    }
    edit.commit();
}

我也做过那样的事情,

Handler ha = new Handler();
    ha.postDelayed(new Runnable() {

        @Override
        public void run() {
            ActivityManager mActivityManager = (ActivityManager) getSystemService("activity");
            List<RunningTaskInfo> list_running = mActivityManager
                    .getRunningTasks(2147483647);