我不知道在哪裏/如何運行此服務在後臺工作。 我開發使用CountDownTimer的應用程序。 我發現我應該在Service.class中創建一個CountDownTimer並在MainActivity.class中運行一個Service.class。如何設置服務/ StartForeground正常工作CountDownTimer
爲什麼TextView不顯示在我的屏幕上?我把它放在OnCreate和onStartCommand中。接下來我嘗試在MainActivity中運行它,但沒有成功。
我仍然嘗試實現我的代碼,以正確地在後臺工作這個CountDownTimer。在官方Android網站上,我看到他們使用「Log.i」命令來運行此Foreground方法。我如何在我的代碼中使用它?我應該在Service.class中添加這個嗎?
下面是我的代碼:
MainActivity:
Button btnStart, btnStop;
TextView textViewTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startService(View view)
{
Intent intent = new Intent(this,MyService.class);
startService(intent);
}
public void stopService(View view)
{
Intent intent = new Intent(this,MyService.class);
stopService(intent);
}
爲MyService:
Button btnStart, btnStop;
TextView textViewTime;
public String hms;
@Override
public void onCreate() {
btnStart = (Button) btnStart.findViewById(R.id.btnStart);
btnStop = (Button) btnStop.findViewById(R.id.btnStop);
textViewTime = (TextView) textViewTime.findViewById(R.id.textViewTime);
textViewTime.setText("00:01:30");
final CounterClass timer = new CounterClass(90000, 1000);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//startService(v);
timer.start();
}
});
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) { // create start a service
textViewTime.setText("00:01:30");
Toast.makeText(this, "Service Started...", Toast.LENGTH_LONG).show();
// timer.start();
//return START_STICKY; // return integer value
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() { // stop a service
Toast.makeText(this, "Service Destroyed...", Toast.LENGTH_LONG).show();
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) { // it's not needed but we must override this method
return null;
}
public class CounterClass extends CountDownTimer {
public CounterClass(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished;
hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
System.out.println(hms);
textViewTime.setText(hms);
}
@Override
public void onFinish() {
textViewTime.setText("Completed.");
}
}
謝謝,@ sector11 但與AppCompatActivity一個問題 - 在我的Android工作室它不顯示,所以我有一個標準的命令ActionBarActivity 。 在CountDownTimerService中我有2個錯誤:「ic.launcher - 無法解析符號」 - 我更改。撤消到mipmap,它已修復。接下來是Intent.FLAG_ACTIVITY_NEW_TASK - 「必須是一個或多個..PendingIntent或Intent ..」。我怎樣才能解決這個問題? – gryzek
添加任何drawable並更改其引用,或者您可以完全評論前景代碼 – sector11
我不知道,但 - 它與Intent.FLAG_ACTIVITY中的一個錯誤一起工作.. - 您對此有何看法?通過這種溝通,我應該再添加一個PendingIntent或Intent。爲什麼? 儘管如此,感謝您的解決方案。有用! :) – gryzek