2016-11-05 49 views
0

我試圖將String轉換爲integerInteger.parseInt()之前工作過,但現在它失敗了。這是在這裏失敗int INTERVAL= (60000 * Integer.parseInt(preferenceTime));我想動態地指定時間,當我安排一個計時器。
謝謝將字符串轉換爲int在服務

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(); 
    } 

} 
+1

你是什麼意思 - 失敗?你有錯誤嗎?意外的結果? – TDG

+1

失敗意味着什麼?它是否拋出任何異常? –

+0

當我構建一個Apk並將其安裝在手機上。並嘗試啓動它。它說「不幸的是,應用程序已停止。」如果我將註釋integer.parseint()出它運行良好。 –

回答

0

Ofcourse它會失敗,因爲你想解析器「」作爲一個int! 試試這個:

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

0意味着,如果我從來沒有在任何東西,返回0。它的默認值。

爲什麼不把它存儲爲int?!我的意思是這樣的:

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

我不存儲它爲int的原因是它是Listpreference。它給了我一個錯誤。它非常感謝你。 –

0

如果它不能解析一個數字,它會拋出一個NumberFormatException異常。我平時喜歡在OptionalInteger#parseInt電話,所以我可以提供一個默認值:

public static Optional<Integer> parseInt(String s) { 
    try { 
     return Optional.of(Integer.parseInt(s)); 
    } catch (NumberFormatException ex) { 
     return Optional.empty(); 
    } 
} 

然後,稱可以提供一個默認的(或者讓你知道它通過#isPresent返回false失敗):

int value = parseInt(preferenceTime).orElse(/* some default integer */); 
+0

只適用於Java 8. – TDG

+0

是的,但您可以輕鬆地添加一個參數並自己傳遞默認值。主要是一個失敗的解析引發'NumberFormatException' – Rogue