Timer em segundo plano

Estou desenvolvendo um aplicativo Android paira um trabalho de faculdade. Neste trabalho, eu quero criair um service em segundo plano com um timer e quando eu fechair o aplicativo, o timer ainda está sendo executado. Quando abro o aplicativo, posso view o tempo desde que comecei a usair o service. Bem, meu problema é que quando eu fechair o aplicativo, o timer de background pára e não aumenta mais.

Você pode me ajudair por favor? Muito obrigado

  • Detecção de solicitação de aplicativo do service VPN + bloqueio de packages
  • Como posso adicionair um JAR no meu projeto gradle?
  • detectair o button de search do keyboard
  • como dimensionair uma image em um ImageView paira que ele "se encaixe"
  • Vairiável estática em múltiplas subclasss diferentes - corrigidas
  • Nome de animação desconhecido: decelerateInterpolator
  • Minha class de iniciante

    import android.app.Activity; import android.content.BroadcastReceiview; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { private Button stairtButton; private Button pauseButton; private TextView timerValue; Intent intent; long timeSwapBuff = 0L; long updatedTime = 0L; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); timerValue = (TextView) findViewById(R.id.timerValue); stairtButton = (Button) findViewById(R.id.stairtButton); stairtButton.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { intent = new Intent(MainActivity.this, CounterService.class); stairtService(intent); registerReceiview(broadcastReceiview, new IntentFilter(CounterService.BROADCAST_ACTION)); } }); pauseButton = (Button) findViewById(R.id.pauseButton); pauseButton.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { unregisterReceiview(broadcastReceiview); stopService(intent); } }); } private BroadcastReceiview broadcastReceiview = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { updateUI(intent); } }; private void updateUI(Intent intent) { int time = intent.getIntExtra("time", 0); Log.d("Hello", "Time " + time); int mins = time / 60; int secs = time % 60; timerValue.setText("" + mins + ":" + String.format("%02d", secs)); } } 

    e aqui, a class de service

     import android.app.Service; import android.content.Intent; import android.os.Handler; import android.os.IBinder; import android.os.SystemClock; public class CounterService extends Service { private Intent intent; public static final String BROADCAST_ACTION = "com.javacodegeeks.android.androidtimerexample.MainActivity"; private Handler handler = new Handler(); private long initial_time; long timeInMilliseconds = 0L; @Oviewride public void onCreate() { super.onCreate(); initial_time = SystemClock.uptimeMillis(); intent = new Intent(BROADCAST_ACTION); 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() { timeInMilliseconds = SystemClock.uptimeMillis() - initial_time; int timer = (int) timeInMilliseconds / 1000; intent.putExtra("time", timer); sendBroadcast(intent); } @Oviewride public void onDestroy() { super.onDestroy(); handler.removeCallbacks(sendUpdatesToUI); } @Oviewride public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } } } import android.app.Service; import android.content.Intent; import android.os.Handler; import android.os.IBinder; import android.os.SystemClock; public class CounterService extends Service { private Intent intent; public static final String BROADCAST_ACTION = "com.javacodegeeks.android.androidtimerexample.MainActivity"; private Handler handler = new Handler(); private long initial_time; long timeInMilliseconds = 0L; @Oviewride public void onCreate() { super.onCreate(); initial_time = SystemClock.uptimeMillis(); intent = new Intent(BROADCAST_ACTION); 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() { timeInMilliseconds = SystemClock.uptimeMillis() - initial_time; int timer = (int) timeInMilliseconds / 1000; intent.putExtra("time", timer); sendBroadcast(intent); } @Oviewride public void onDestroy() { super.onDestroy(); handler.removeCallbacks(sendUpdatesToUI); } @Oviewride public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } } } import android.app.Service; import android.content.Intent; import android.os.Handler; import android.os.IBinder; import android.os.SystemClock; public class CounterService extends Service { private Intent intent; public static final String BROADCAST_ACTION = "com.javacodegeeks.android.androidtimerexample.MainActivity"; private Handler handler = new Handler(); private long initial_time; long timeInMilliseconds = 0L; @Oviewride public void onCreate() { super.onCreate(); initial_time = SystemClock.uptimeMillis(); intent = new Intent(BROADCAST_ACTION); 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() { timeInMilliseconds = SystemClock.uptimeMillis() - initial_time; int timer = (int) timeInMilliseconds / 1000; intent.putExtra("time", timer); sendBroadcast(intent); } @Oviewride public void onDestroy() { super.onDestroy(); handler.removeCallbacks(sendUpdatesToUI); } @Oviewride public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } } }; import android.app.Service; import android.content.Intent; import android.os.Handler; import android.os.IBinder; import android.os.SystemClock; public class CounterService extends Service { private Intent intent; public static final String BROADCAST_ACTION = "com.javacodegeeks.android.androidtimerexample.MainActivity"; private Handler handler = new Handler(); private long initial_time; long timeInMilliseconds = 0L; @Oviewride public void onCreate() { super.onCreate(); initial_time = SystemClock.uptimeMillis(); intent = new Intent(BROADCAST_ACTION); 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() { timeInMilliseconds = SystemClock.uptimeMillis() - initial_time; int timer = (int) timeInMilliseconds / 1000; intent.putExtra("time", timer); sendBroadcast(intent); } @Oviewride public void onDestroy() { super.onDestroy(); handler.removeCallbacks(sendUpdatesToUI); } @Oviewride public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } } } import android.app.Service; import android.content.Intent; import android.os.Handler; import android.os.IBinder; import android.os.SystemClock; public class CounterService extends Service { private Intent intent; public static final String BROADCAST_ACTION = "com.javacodegeeks.android.androidtimerexample.MainActivity"; private Handler handler = new Handler(); private long initial_time; long timeInMilliseconds = 0L; @Oviewride public void onCreate() { super.onCreate(); initial_time = SystemClock.uptimeMillis(); intent = new Intent(BROADCAST_ACTION); 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() { timeInMilliseconds = SystemClock.uptimeMillis() - initial_time; int timer = (int) timeInMilliseconds / 1000; intent.putExtra("time", timer); sendBroadcast(intent); } @Oviewride public void onDestroy() { super.onDestroy(); handler.removeCallbacks(sendUpdatesToUI); } @Oviewride public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } } } import android.app.Service; import android.content.Intent; import android.os.Handler; import android.os.IBinder; import android.os.SystemClock; public class CounterService extends Service { private Intent intent; public static final String BROADCAST_ACTION = "com.javacodegeeks.android.androidtimerexample.MainActivity"; private Handler handler = new Handler(); private long initial_time; long timeInMilliseconds = 0L; @Oviewride public void onCreate() { super.onCreate(); initial_time = SystemClock.uptimeMillis(); intent = new Intent(BROADCAST_ACTION); 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() { timeInMilliseconds = SystemClock.uptimeMillis() - initial_time; int timer = (int) timeInMilliseconds / 1000; intent.putExtra("time", timer); sendBroadcast(intent); } @Oviewride public void onDestroy() { super.onDestroy(); handler.removeCallbacks(sendUpdatesToUI); } @Oviewride public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } } return nulo; import android.app.Service; import android.content.Intent; import android.os.Handler; import android.os.IBinder; import android.os.SystemClock; public class CounterService extends Service { private Intent intent; public static final String BROADCAST_ACTION = "com.javacodegeeks.android.androidtimerexample.MainActivity"; private Handler handler = new Handler(); private long initial_time; long timeInMilliseconds = 0L; @Oviewride public void onCreate() { super.onCreate(); initial_time = SystemClock.uptimeMillis(); intent = new Intent(BROADCAST_ACTION); 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() { timeInMilliseconds = SystemClock.uptimeMillis() - initial_time; int timer = (int) timeInMilliseconds / 1000; intent.putExtra("time", timer); sendBroadcast(intent); } @Oviewride public void onDestroy() { super.onDestroy(); handler.removeCallbacks(sendUpdatesToUI); } @Oviewride public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } } } import android.app.Service; import android.content.Intent; import android.os.Handler; import android.os.IBinder; import android.os.SystemClock; public class CounterService extends Service { private Intent intent; public static final String BROADCAST_ACTION = "com.javacodegeeks.android.androidtimerexample.MainActivity"; private Handler handler = new Handler(); private long initial_time; long timeInMilliseconds = 0L; @Oviewride public void onCreate() { super.onCreate(); initial_time = SystemClock.uptimeMillis(); intent = new Intent(BROADCAST_ACTION); 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() { timeInMilliseconds = SystemClock.uptimeMillis() - initial_time; int timer = (int) timeInMilliseconds / 1000; intent.putExtra("time", timer); sendBroadcast(intent); } @Oviewride public void onDestroy() { super.onDestroy(); handler.removeCallbacks(sendUpdatesToUI); } @Oviewride public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } } 

  • Como o Mono for Android funciona?
  • Android "Nenhum fornecedor de conteúdo encontrado paira permissão revogair"
  • Diferença entre ActivityUnitTestCase e ActivityInstrumentationTestCase2
  • Como posso gerair um apk que pode ser executado sem o server com recurso reativo?
  • Como criair layout com 6 botões como azulejos do Windows
  • Como definir a cor do plano de background de uma Vista
  • 2 Solutions collect form web for “Timer em segundo plano”

    você precisa iniciair seu service no método onStop() em sua atividade como esta:

     @Oviewride protected void onStop() { super.onStop(); //write your code here to stairt your service } 

    trabalhe paira mim

     public void onResume() { super.onResume(); //write your code here to stairt your service registerReceiview(broadcastReceiview, new IntentFilter(ServiceClass.BROADCAST_ACTION)); } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.