1

当我启动应用程序时,计数器中的值每 1000 毫秒发布一次。即使应用程序关闭或屏幕锁定,该服务也会继续运行,但是当我再次启动应用程序时,服务会重新创建,我看到计数器从 0 开始,它在旧计数器值和新计数器值之间随机播放。

所以我看到 2 个值 - 新计数器和旧计数器值同时更新并重叠。如何消除新计数器(或停止重新创建服务)

这是我在后台运行的服务

package com.myexample.serviceexample;

import java.util.Calendar;
import java.util.Date;

import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

public class myservice extends Service {

    private static final String TAG = "serviceexample";
    public static final String BROADCAST_ACTION = "com.myexample.serviceexample";
    private final Handler handler = new Handler();
    Intent intent;
    int counter = 0;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        intent = new Intent(BROADCAST_ACTION);    
    }

    @Override
      public int onStartCommand(Intent intent, int flags, int startId) {

        handler.removeCallbacks(sendUpdatesToUI);
        handler.postDelayed(sendUpdatesToUI,1000);
        return Service.START_STICKY;
    }


    private Runnable sendUpdatesToUI = new Runnable() {
        public void run() {
            DisplayLoggingInfo();           
            handler.postDelayed(this,1000); 
        }
    };  

    private void DisplayLoggingInfo() {
        Log.d(TAG, "entered DisplayLoggingInfo");

        intent.putExtra("time","some text" );
        intent.putExtra("counter", String.valueOf(++counter));
        sendBroadcast(intent);
    }
}

这是我的活动代码

package com.myexample.serviceexample;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.os.Build;

public class MainActivity extends Activity {

    private static final String TAG = "MainActivity";
    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        intent = new Intent(this, myservice.class);

    }

    public void onClick(View view) {

    }

    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            updateUI(intent);       
        }
    }; 

    public void onResume() {
        super.onResume();        
        startService(intent);
        registerReceiver(broadcastReceiver, new IntentFilter(myservice.BROADCAST_ACTION));
    }
    public void onStop() {
        super.onStop();        
        startService(intent);
        registerReceiver(broadcastReceiver, new IntentFilter(myservice.BROADCAST_ACTION));
    }

    @Override
    public void onPause() {
        super.onPause();
        unregisterReceiver(broadcastReceiver);
        stopService(intent);         
    }

    private void updateUI(Intent intent) {
        String counter = intent.getStringExtra("counter"); 
        String time = intent.getStringExtra("time");
        Log.d(TAG, counter);
        Log.d(TAG, time);

        TextView txtDateTime = (TextView) findViewById(R.id.textView1);      
        TextView txtCounter = (TextView) findViewById(R.id.textView2);
        txtDateTime.setText(time);
        txtCounter.setText(counter);
    }

}
4

1 回答 1

0

它现在工作得很好。

我所做的更改:

  1. 删除 onPause() 函数
  2. 从 onResume() 中删除 startService(intent),这样它就不会在我每次打开应用程序时重新创建
  3. 将 startService(intent) 放入 onCreate() 函数中
于 2014-07-03T19:04:56.797 回答