我試圖將String
轉換爲integer
。 Integer.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();
}
}
你是什麼意思 - 失敗?你有錯誤嗎?意外的結果? – TDG
失敗意味着什麼?它是否拋出任何異常? –
當我構建一個Apk並將其安裝在手機上。並嘗試啓動它。它說「不幸的是,應用程序已停止。」如果我將註釋integer.parseint()出它運行良好。 –