2013-11-02 23 views
13

私は大学の仕事にAndroidアプリケーションを開発しています。この作業では、タイマーを使用してバックグラウンドサービスを作成したいと思います。アプリケーションを閉じると、タイマーはまだ実行されています。アプリを開くと、サービスを開始してからの時間がわかります。 私の問題は、私がアプリを閉じると、バックグラウンドタイマーが止まり、さらに増えないということです。Timer in Background

お願いします。 どうもありがとう

マイランチャークラス

import android.app.Activity; 
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.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class MainActivity extends Activity { 

    private Button startButton; 
    private Button pauseButton; 

    private TextView timerValue; 

    Intent intent; 
    long timeSwapBuff = 0L; 
    long updatedTime = 0L; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     timerValue = (TextView) findViewById(R.id.timerValue); 

     startButton = (Button) findViewById(R.id.startButton); 
     startButton.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View view) { 
       intent = new Intent(MainActivity.this, CounterService.class); 
       startService(intent); 
       registerReceiver(broadcastReceiver, new IntentFilter(CounterService.BROADCAST_ACTION)); 
      } 
     }); 

     pauseButton = (Button) findViewById(R.id.pauseButton); 

     pauseButton.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View view) { 
       unregisterReceiver(broadcastReceiver); 
       stopService(intent); 
      } 
     }); 

    } 

    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { 
     @Override 
     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)); 
    } 


} 

、ここで、

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; 

    @Override 
    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); 

    } 

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

    } 



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


} 
+0

このために「サービス」を使用する必要がありますか?あなたはいつでもミリ秒単位で(SharedPreferencesでもローカルSQLデータベースに)ミリ秒単位で時間を保存し、 'onResume()'の中でいくつかのクイックデルタ算術演算を実行して、あなたが行っているアクション測定しようとしました。 – Squagem

+0

私は時間を示す通知をするために、サービスを利用しようとしています。しかしあなたのアイデアはまったく悪いことではありません – Luis

+0

'Service'クラスについてもっと学ぶことに興味があるなら(通常はカレッジクラスの場合と同じですが)、@Mahfaが提供する答えを見てください。そうでなければ、私の提案ははるかに簡単になると信じています。 – Squagem

答えて

7

は、あなたがこのようなあなたの活動にonStop()方法であなたのサービスを開始する必要があるサービスクラス:

@Override 
    protected void onStop() { 
     super.onStop(); 
     //write your code here to start your service 
    } 
0

私の仕事

public void onResume() { 
    super.onResume(); 

    //write your code here to start your service 

    registerReceiver(broadcastReceiver, new IntentFilter(ServiceClass.BROADCAST_ACTION)); 
} 
0
@Override 
protected void onStop() { 
    super.onStop(); 
    //write your code here to start your service 
} 

私はこれがあなたのために働くべきだと思います。