2016-08-14 3 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() 전에 호출됩니다.

AFAIK :

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();" –