活动关闭时刷新服务和通知

时间:2015-07-22 10:19:49

标签: java android xml

我有一个带有进度条通知的服务。当我打开主要活动并且工作正常时启动此服务。但是,当我关闭主活动(调用onDestroy())时,通知就像刷新一样。这是我服务的代码。

@Override
    public void onCreate() {
        super.onCreate();               

        Mypreferences prefs = new Mypreferences(this);

        String minutes = prefs.getStringValueOf("Minutes");

        final NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        final NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

        d = Integer.valueOf(minutes)*60;

        builder.setSmallIcon(R.drawable.ic_action_about);
        builder.setContentTitle(getString(R.string.app_name));
        new Thread(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                for(int i=0;i<d;i++) {
                    builder.setProgress(d, d-i, false);
                    builder.setContentInfo(d-i+"/"+d);
                    notificationManager.notify(0, builder.build());
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }               
        }).start();

整数60它和倒计时工作正常,但当我关闭MainActivity时,如果例如ProgressBar为40,则返回60.为什么?我怎么解决?

服务代码

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

@Override
public void onCreate() {
    super.onCreate();
    pref = new SharedPref(this);
    map = pref.getAllPrefs();

    boolean delay = pref.loadBooleanPref("delay");
    String delayTime = pref.loadStringValue("DelayTime");

    if(delay && delayTime != null) {
        new CountDownTimer(TimeUnit.MINUTES.toMillis(Long.valueOf(delayTime)), 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                // TODO Auto-generated method stub      
            }

            @Override
            public void onFinish() {
                // TODO Auto-generated method stub
                getInfo();
            }

        }.start();
    } else {
        getInfo();
    }

}

0 个答案:

没有答案