2016-08-14 1 views
0

私がする必要があるのは、ユーザーが何秒も入力してボタンを押すとサービスが開始され、画面が消えるアプリケーションを作成することです。それぞれの秒数が経過すると、画面がオンになり、サービスが停止します。入力した秒数後にサービスを停止しようとしています

現在のところ、私が入力したものは何でも、サービスは停止します。なぜどんなアイデア?

MainActivity(切り捨て):

final EditText timer = (EditText) findViewById(R.id.insertTimer); 

    findViewById(R.id.startApp).setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(MainActivity.this, MainService.class); 
      intent.putExtra("timer", timer.getText().toString()); 
      startService(intent); 

      Toast.makeText(MainActivity.this, "Countdown, Started", Toast.LENGTH_SHORT).show(); 

      Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 1000); 
     } 
    }); 

MainService:

public class MainService extends Service { 

    String usedTimer; 
    long interval; 

    TimerTask myTask = new TimerTask() { 
     public void run() { 
      stopSelf(); 
     } 
    }; //TimerTask that will cause the run() runnable to happen. 
    Timer myTimer = new Timer(); //Timer that will make the runnable run. 

    private static final String TAG = "HelloService"; 
    private boolean isRunning = false; 

    @Override 
    public void onCreate() { 
     registerReceiver(counter, new IntentFilter(Intent.ACTION_SCREEN_ON)); 
     myTimer.schedule(myTask, interval); 

     Log.i(TAG, "Service, onCreate"); 
     Toast.makeText(MainService.this, "Service, Created", Toast.LENGTH_SHORT).show(); 

     isRunning = true; 
    } 

    private BroadcastReceiver counter = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      Toast.makeText(MainService.this, "Whoops! You've Lost.", Toast.LENGTH_SHORT).show(); 
     } 
    }; 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     usedTimer = intent.getStringExtra("timer"); 

     try { 
      interval = Long.parseLong(usedTimer); 
     } catch(NumberFormatException nfe) { 
      System.out.println("NumberFormatException: " + nfe.getMessage()); 
     } 

     Log.i(TAG, "Service, onStartCommand"); 
     Toast.makeText(MainService.this, usedTimer, Toast.LENGTH_SHORT).show(); 
     return super.onStartCommand(intent, flags, startId); 
    } 

    @Override 
    public void onDestroy() { 
     Log.i(TAG, "Service, onDestroy"); 

     PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 
     PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK 
       | PowerManager.ACQUIRE_CAUSES_WAKEUP 
       | PowerManager.ON_AFTER_RELEASE, "MyWakeLock"); 
     wakeLock.acquire(); 

     Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 30000); 

     android.os.Process.killProcess(android.os.Process.myPid()); 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     Log.i(TAG, "Service, onBind"); 
     return null; 
    } 
} 

答えて

2

それが原因で、この行は次のとおりです。

myTimer.schedule(myTask, interval); 

この段階では、intervalが初期化されていないonCreate()を理由onStartCommand()の前に呼び出されます。

私の知る限り:

Intent i = new Intent(this, YourService.class) // onCreate() called 
startService(i); // onStartCommand() called 
+1

YES!ありがとうございました!私がしたのは移動することだった "myTimer.schedule(myTask、interval);" onCreateからonStartCommandまで、 "Toast.makeText(MainService.this、usedTimer、Toast.LENGTH_SHORT).show();" –

関連する問題