Android i wyłączenie timera po wyjściu z aplikacji

0

Witam,

Mam kod, który uruchamia Timer i mierzy czas. Wszystko działa prawidłowo dopóki użytkownik przez przypadek nie wyjdzie z aplikacji. Zegar staje i po ponownym wejściu trzeba wszystko uzupełniać od nowa. Jak zrobić, żeby "działa w tle"?

 myTimer = new Timer();
        myTimer.schedule(new TimerTask() {			
			@Override
			public void run() {
				TimerMethod();
			}			
		}, 0, 1000);   

private void TimerMethod()
	{    	
		this.runOnUiThread(Timer_Tick);
	}

	private Runnable Timer_Tick = new Runnable() {
		public void run() {
			if (zliczanie)
				czas++;			
			TextView tvc = (TextView)findViewById(R.id.pokaz_czas);
			tvc.setText("" + FormatujCzas(czas));
		}
	};
	
	static String FormatujCzas(int secsIn)
	{
		int minutes = secsIn / 60,	seconds = secsIn % 60;		
		return ( (minutes < 10 ? "0" : "") + minutes + ":" + (seconds< 10 ? "0" : "") + seconds );
	}
		
0

Poczytaj o: http://developer.android.com/reference/android/app/Service.html
Musisz w manifeście aplikacji (albo w kodzie) zarejestrować taki serwis. Będzie on działać nawet po zakończeniu aplikacji.

0

No niby mam zrobiony service ale jak wyjdę z aplikacji to zaczyna odliczać od zera:
Activity:

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);                                                      
        setContentView(R.layout.czas);                     
        intent = new Intent(this, ServiceTimer.class);        
    }                    
	
    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
        	updateUI(intent);       
        }
    };    
    
	@Override
	public void onResume() {
		super.onResume();		
		startService(intent);
		registerReceiver(broadcastReceiver, new IntentFilter(ServiceTimer.BROADCAST_ACTION));
	}
	
	@Override
	public void onPause() {
		super.onPause();
		unregisterReceiver(broadcastReceiver);
		stopService(intent); 		
	}
	    
    private void updateUI(Intent intent) {
    	String counter = intent.getStringExtra("counter");    
    	int czas_nowy = Integer.parseInt(counter);    	
    	TextView tvc = (TextView)findViewById(R.id.pokaz_czas);
		tvc.setText("" + FormatujCzas(czas + czas_nowy));
    }

Service:

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

public class ServiceTimer  extends Service {
	private static final String TAG = "BroadcastService";
	public static final String BROADCAST_ACTION = "package";
	private final Handler handler = new Handler();
	Intent intent;
	int counter = 0;
	
	@Override
	public void onCreate() {
		super.onCreate();

    	intent = new Intent(BROADCAST_ACTION);	
	}
	
    @Override
    public void onStart(Intent intent, int startId) {
        handler.removeCallbacks(sendUpdatesToUI);
        handler.postDelayed(sendUpdatesToUI, 1000); // 1 second
   
    }

    private Runnable sendUpdatesToUI = new Runnable() {
    	public void run() {
    		DisplayLoggingInfo();    		
    	    handler.postDelayed(this, 1000); // 1 seconds
    	}
    };    
    
    private void DisplayLoggingInfo() {
    	Log.d(TAG, "entered DisplayLoggingInfo");        	
    	intent.putExtra("counter", String.valueOf(++counter));
    	sendBroadcast(intent);
    }
	
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onDestroy() {		
        handler.removeCallbacks(sendUpdatesToUI);		
		super.onDestroy();
	}		
}
0

Tak jak napisałem, program się kompiluje, uruchamia, działa prawidłowo ale po wyjściu i ponownym uruchomieniu licznik zaczyna od nowa.

0

Musisz zbindować aktywność z serwisem. Wiele na ten temat Ci nie powiem, bo sam w Androidzie zaawansowany nie jestem. Jestem pewien, że ma to związek z funkcją onBind.

1 użytkowników online, w tym zalogowanych: 0, gości: 1