No problem, here is the code:
Basically when the timer reaches 0:00, then another network should get connected.
package com.example.yakir.test;
import android.content.Context;
import android.os.CountDownTimer;
import android.support.annotation.RequiresPermission;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView logView;
private Button button;
private TextView timer;
private CountDownTimer countDownTimer;
private long timeLeftInMillisecond = 10000; //10 minutes
private boolean timerRunning;
private TelephonyManager tm;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//setting the views
logView = (TextView)findViewById(R.id.logView);
button = (Button)findViewById(R.id.button);
timer = (TextView)findViewById(R.id.timer);
logView.setText("Recent tests:");
//initializing telephonymanager
tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
//onClickListener for the button
/*
start the countdown
*/
button.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View view) {
startStop();
}
});
}
/**
* Timer methods -->
*/
//for the timer to toggle start and stop
public void startStop(){
if(timerRunning){
stopTimer();
}else{
startTimer();
}
}
public void startTimer(){
countDownTimer = new CountDownTimer(timeLeftInMillisecond,1000) {
@override
public void onTick(long l) {
timeLeftInMillisecond = l;
updateTimer();
}
@override
public void onFinish() {
//switching to a different network by mpln
try {
boolean networkChanged = tm.setNetworkSelectionModeManual("USAW6", false);
}catch(Exception e){
System.out.println("Error");
}
//restart timer
countDownTimer.start();
}
}.start();
button.setText("Pause Testing");
timerRunning = true;
}
public void stopTimer(){
countDownTimer.cancel();
button.setText("Start Scheduled Test");
timerRunning = false;
}
public void updateTimer(){
int minutes = (int)timeLeftInMillisecond/60000;
int seconds = (int)timeLeftInMillisecond%60000/1000;
String timeLeftText = minutes+":";
if(seconds<10){
timeLeftText += "0";
}
timeLeftText+=seconds;
timer.setText(timeLeftText);
}
// timer methods <--
}