有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java调用电话号码列表并返回活动

我想调用数组中的一些数字。所以我把它们放在一个循环中,并以一个调用应用程序的意图开始。使用phonecalllistener,当通话结束时,我返回到我的活动,但我的问题是循环从begin开始,我必须再次单击按钮

我可以用捆绑解决这个问题吗?怎么做? 当活动重新启动时,如何保存循环状态

我还在清单中写入了正确的权限(CALL_PHONE和READ_PHONE_STATE)

public class MainActivity extends Activity {

final Context context = this;

private Button button;
public int g = 0;
public String[] nummern = new String[10];


public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button = (Button) findViewById(R.id.buttonCall);
    nummern[0] = "tel:06746468156";
    nummern[1] = "tel:06991046460";
    nummern[2] = "tel:06504146464";

    PhoneCallListener phoneListener = new PhoneCallListener();
    TelephonyManager telephonyManager = (TelephonyManager) this
            .getSystemService(Context.TELEPHONY_SERVICE);
    telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);

    // add button listener
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
                for (g = 0; g <= nummern.length-1; g++) {

                    Intent callIntent = new Intent(Intent.ACTION_CALL);

                    callIntent.setData(Uri.parse(nummern[g]));
                    startActivity(callIntent);

                }


        }

    });

}

private class PhoneCallListener extends PhoneStateListener {

    private boolean isPhoneCalling = false;

    String LOG_TAG = "LOGGING 123";


    @Override
    public void onCallStateChanged(int state, String incomingNumber) {

        if (TelephonyManager.CALL_STATE_RINGING == state) {
            // phone ringing
            Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
        }

        if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
            // active
            Log.i(LOG_TAG, "OFFHOOK");

            isPhoneCalling = true;
        }

        if (TelephonyManager.CALL_STATE_IDLE == state) {
            // run when class initial and phone call ended,
            // need detect flag from CALL_STATE_OFFHOOK
            Log.i(LOG_TAG, "IDLE");

            if (isPhoneCalling) {

                Log.i(LOG_TAG, "restart app");

                // restart app
                Intent i = getBaseContext().getPackageManager()
                        .getLaunchIntentForPackage(
                                getBaseContext().getPackageName());
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);

                isPhoneCalling = false;
            }

        }
    }
}

}


共 (1) 个答案

  1. # 1 楼答案

    从您的按钮单击侦听器调用startActivityForResult(Intent-Intent,int-requestCode),第一个数字应作为意图数据传递,一旦调用完成,将调用“onActivityResult(int-requestCode,int-resultCode,Intent-data)”方法,您可以从此方法再次启动活动。您可以相应地应用逻辑