ilia sidamonidze ilia sidamonidze - 1 month ago 11
Java Question

Convert String to int in Service

I am trying to convert String into integer. Integer.parseInt() worked before, but now it is failing. It is failing here int INTERVAL= (60000 * Integer.parseInt(preferenceTime)); I am trying dynamically specify time when I schedule a timer.

thank you

public class Service extends Service {

public SharedPreferences settings;


private Handler HandleIt = new Handler();
private Timer timer = new Timer();
boolean timeout = false;
//private PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE);





@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}

//////////////////////////////////////////////

class TimeDisplayTimerTask extends TimerTask {



@Override
public void run() {
HandleIt.post(new Runnable(){
public void run(){

//SharedPreferences
settings = getSharedPreferences("timer_preference", MODE_PRIVATE);
String preferenceTime = settings.getString("timer_preference", "");

// int INTERVAL= (60000 * Integer.parseInt(preferenceTime));


Toast.makeText(getApplicationContext(), TextonScreen(), Toast.LENGTH_SHORT).show();

//get screen light up
PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isScreenOn();
if(isScreenOn==false) {
pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "My Tag");
}
// make a new intent and start it with flag and send an sms
Intent launch = new Intent(getBaseContext(), SMS.class);
launch.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(launch);
}
});
}


}

private String TextonScreen()
{
timeout = true;
return "it is running";


}
boolean isTimeout()
{
return timeout;
}
@Override
public void onCreate() {

// TODO Auto-generated method stub
super.onCreate();
Toast.makeText(this, "Service is created", Toast.LENGTH_SHORT).show();



}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {


SharedPreferences settings = getSharedPreferences( getPackageName() + "timer_preference", MODE_PRIVATE);
String preferenceTime = settings.getString("timer_preference", "");
int INTERVAL= (60000 * Integer.parseInt(preferenceTime));



// TODO Auto-generated method stub
// Display the Toast Message
Toast.makeText(this, "Start Service", Toast.LENGTH_SHORT).show();
// Execute an action after period time
//comes from the TimeDisplayTimerTask class
timer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 0, INTERVAL);


return super.onStartCommand(intent, flags, startId);
}


@Override
public void onDestroy() {
// Display the Toast Message
Toast.makeText(this, "Stop Service", Toast.LENGTH_SHORT).show();
if (timer != null) {
timer.cancel();
}
super.onDestroy();
}


}

Answer

Ofcourse it would fail because you want to parser "" as an int ! Try this :

String preferenceTime = settings.getString("timer_preference", "0");
int INTERVAL=  (60000 * Integer.parseInt(preferenceTime));

0 means if i never set anything in it, return 0. Its the default value.

why aren't you storing it as an int ?! i mean this :

int preferenceTime = settings.getInteger("timer_preference", 0);
int INTERVAL=  (60000 * preferenceTime);