【发布时间】:2016-07-28 11:19:28
【问题描述】:
我想调用数组中的一些数字。所以我把它们放在一个循环中,并从调用应用程序的意图开始。使用 phonecalllistener 通话结束后我会回到我的活动,但我的问题是循环从头开始,我必须再次单击按钮。
我可以用 Bundle 解决这个问题吗?如何? Activity 重新启动时如何保存我的循环状态?
我还在清单中写了正确的权限(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;
}
}
}
}
}
【问题讨论】:
-
只需保存您的循环状态,或已拨打的电话号码
-
Activity 重新启动时如何保存我的循环状态?
-
如果我理解您的代码正确,您在通话结束后重新启动应用程序?你为什么这么做 ?您可以从那里拨打下一个号码,而不是重新启动应用程序
标签: java android android-intent call bundle