2014-06-04 81 views
0

我有一個服務:安卓:服務沒有運行

import android.app.IntentService; 
import android.content.Intent; 
import android.os.CountDownTimer; 
import android.widget.Button; 

public class CustTimer extends IntentService { 
int length; 
long timeLeft; 
Button button; 
long endTime; 

public CustTimer() { 
    super("CustTimer"); 
} 

public CustTimer(int length, Button button) { 
    super("CustTimer"); 
    this.length = length; 
    this.button = button; 
    this.endTime = System.currentTimeMillis() + length; 


} 


@Override 
protected void onHandleIntent(Intent intent) { 
    // TODO Auto-generated method stub 
new CountDownTimer(length, 1000) { 

     public void onTick(long millisUntilFinished) { 

      String min = String.valueOf(millisUntilFinished/60000); 

      long re = millisUntilFinished%60000; 
      String secs = String.valueOf(re/1000); 

      button.setText(min + ":" + secs); 
      timeLeft = millisUntilFinished; 
     } 

     public void onFinish() { 
      //button.setText("done"); 
     } 

    }.start(); 

} 
} 

並調用它的活動中,onCreate方法是:

protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 

    final Intent intent = new Intent(this, CustTimer.class); 
    LinearLayout ll = new LinearLayout(this); 
    ll.setOrientation(LinearLayout.VERTICAL); 
    LinearLayout l2 = new LinearLayout(this); 
    l2.setOrientation(LinearLayout.HORIZONTAL); 
    LinearLayout l3 = new LinearLayout(this); 
    l3.setOrientation(LinearLayout.HORIZONTAL); 
    LinearLayout l4 = new LinearLayout(this); 
    l4.setOrientation(LinearLayout.HORIZONTAL); 
    LinearLayout l5 = new LinearLayout(this); 
    l5.setOrientation(LinearLayout.HORIZONTAL); 

    LayoutParams hor = new LayoutParams(0, LayoutParams.MATCH_PARENT, 1f); 
    LayoutParams ver = new LayoutParams(LayoutParams.MATCH_PARENT, 0, 1f); 


    for (int i = 0; i < 16; i++){ 
     buts[i] = new Button(this); 
     buts[i].setText(String.valueOf(i+1)); 
     timerRun[i] = false; 
    } 

    for (int i = 0; i < 4; i++) { 
     l2.addView(buts[i], hor); 
    } 
    for (int i = 4; i < 8; i++) { 
     l3.addView(buts[i], hor); 
    } 
    for (int i = 8; i < 12; i++) { 
     l4.addView(buts[i], hor); 
    } 
    for (int i = 12; i < 16; i++) { 
     l5.addView(buts[i], hor); 
    } 

    for (int i = 0; i < 16; i++) { 
      final int j = i; 
     buts[i].setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 

       CustTimer t1 = new CustTimer(times[j], buts[j]); 
       timers[j] = t1; 
       startService(intent); 
      } 
     }); 
    } 

    ll.addView(l2, ver); 
    ll.addView(l3, ver); 
    ll.addView(l4, ver); 
    ll.addView(l5, ver); 

    this.setContentView(ll); 
} 

我的問題是,startService()調用沒有按」但不會返回任何錯誤,但不會運行。只是在尋找一些基本的輸入上,如果我錯過了什麼重要的事做與服務

編輯: 清單段是:

<service android:name="CustTimer"> </service> 
+0

您可以發佈定義您的服務的AndroidManifest.xml部分嗎? – bstar55

回答

0

有實現一個簡單的計時器一個簡單的方法:

private final Handler mHandler = new Handler(); 
private final Runnable mRunnable = new Runnable() { 
    @Override 
    public void run() { 
     // TODO: update button text, manipulate views, etc. 

     mHandler.postDelayed(mRunnable, 1000); 
    } 
}; 

private void start() { 
    mHandler.postDelayed(mRunnable, 1000); 
} 

private void stop() { 
    mHandler.removeCallbacks(mRunnable); 
} 
+0

謝謝,我的計時器是寫在一個服務,以便它將繼續運行時,我暫時離開活動,仍然運行時,當我回到活動 – sam