如何在android中运行后台程序?

时间:2012-04-05 20:11:20

标签: android

我有这段代码:

package com.powergroupbd.timer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class TimerActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */

    EditText minute, seconds;
    Button fire;
    String min, sec;
    TextView Remain;
    MyCount timercount;
    Intent serviceIntent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initializer();
        fire.setOnClickListener(this);
    }

    private void initializer() {

        minute = (EditText) findViewById(R.id.etMinute);
        seconds = (EditText) findViewById(R.id.etSeconds);
        fire = (Button) findViewById(R.id.bFireTimer);
        Remain = (TextView) findViewById(R.id.tvRemain);
        serviceIntent = new Intent(TimerActivity.this, MyService.class);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        case R.id.bFireTimer:

            min = minute.getText().toString();
            sec = seconds.getText().toString();
            int MIN = Integer.parseInt(min);
            int SEC = Integer.parseInt(sec);
            long TIME = (MIN * 60) + SEC;
            timercount = new MyCount(TIME * 1000, 1000);
            timercount.start();

            startService(serviceIntent);

            break;
        }

    }

    public class MyCount extends CountDownTimer {

        public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            // some script here

            Remain.setText("0:0");
        }

        @Override
        public void onTick(long millisUntilFinished) {

            // some script here
            int remseconds = (int) millisUntilFinished / 1000;
            int remminuts = remseconds / 60;
            remseconds = remseconds % 60;
            remminuts = remminuts % 60;
            Remain.setText(String.format("%d : %02d", remminuts, remseconds));

            // Remain.setText(""+millisUntilFinished);

        }
    }

}

我在edittext中放入一些数值,并从该值倒数到零。但是,当我重新启动这个应用程序时,它从开始开始,并且必须再次放置这些值。但是当我不在那个应用程序中时,我想在后台进行倒计时。我在这里使用了服务,我的服务类代码在这里:

package com.powergroupbd.timer;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class MyService extends Service {



    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }


    @Override
    public void onStart(Intent intent, int startId) {
        // TODO Auto-generated method stub
        super.onStart(intent, startId);
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

}

但是什么都没有,现在该怎么办?

2 个答案:

答案 0 :(得分:1)

一般来说,没有办法不断在后台运行。当内存不足时,操作系统会终止后台服务。尝试与持久性变量(例如存储在AlarmManager中)中的警报(如Preferences中所述)。

答案 1 :(得分:0)

听起来就像你希望计数在后台继续,这可以通过后台进程完成。我假设在android api中有更好的方法,但是

Simialar stackoverflow question on services

如果您只是希望计数器在您离开的位置重新启动,则需要管理您的生命周期。退出时保存该号码并在启动时重新加载。

Process Lifecycle documentation