0
我想調用數組中的某些數字。所以我把它們放在一個循環中,並開始了一個意圖的通話應用程序。使用phonecalllistener時,我會在通話結束時返回到我的活動,但是我的問題是循環從頭再次開始,我必須再次單擊該按鈕。調用電話號碼列表並返回到活動
我可以用一個包解決這個問題嗎?如何? 如何在活動重新啓動時保存我的循環狀態?
我也寫在清單正確的權限(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;
}
}
}
}
}
Juste保存您的循環狀態或已撥打哪個電話號碼 –
如何在活動重新啓動時保存我的循環狀態? – Marco
如果我正確理解您的代碼,請在完成通話後重新啓動應用程序?你爲什麼這麼做 ?你可以從那裏撥打下一個號碼,而不是重新啓動應用程序 – JonasCz