• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

telephonymanager to connect to networks

ykrgby

Lurker
Hey everyone, trying to use the method: setNetworkSelectionModeManual
from the telephonymanager library.
It closes the app whenever I launch it..
It also requires a permission: MODIFY_PHONE_STATE, which I put in the manifest
then there was an error mentioning that this is a system permission and this app is not a system app.
I set Android Studio to ignore this warning, however I do need for some way to launch this method.
Maybe the only phones it could run on are rooted ones?

Would be appreciate if anyone could throw some light at this problem.
 
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 <--
}
 
Back
Top Bottom