Android:服务未运行

时间:2014-06-04 23:40:56

标签: java android android-service

我有服务:

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>

1 个答案:

答案 0 :(得分: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);
}