0
私はアプリから戻ったり、アプリ内の別の画面に移動するとカウントダウンタイマー画面をカウントダウンし続けます。私はそれをサービスとして実行していますが、アクティビティを再度開いたときにはじめて問題が発生しています。どんな助けも素晴らしいだろう。ここに私のコードです。主な活動のためのアクティビティ画面を再開すると、CountdownTimerサービスが再開されます
サービスクラス
import android.app.Service;
import android.content.Intent;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
public class CountdownService extends Service{
public static final String
ACTION_LOCATION_BROADCAST = CountdownService.class.getName() + "LocationBroadcast";
@Override
public void onCreate() {
super.onCreate();
new CountDownTimer(360000, 60000) {
public void onTick(long millisUntilFinished) {
int timeLeftInt = (int) Math.ceil((double) millisUntilFinished/60000); //Whole number of minutes left, ceiling
sendBroadcastMessage(timeLeftInt);
if(timeLeftInt == 5){
Notify("Not Done");
}
}
public void onFinish() {
sendBroadcastMessage(0);
Notify("done");
}
}.start();
}
private void sendBroadcastMessage(int timeSent) {
Intent intent = new Intent(ACTION_LOCATION_BROADCAST);
intent.putExtra("timeSent", timeSent);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void Notify(String doneness){
NotificationManager notificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Intent intent = new Intent(this, Map.class);
PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);
if(doneness.equals("done")) {
Notification n = new Notification.Builder(this)
.setContentTitle("Time to leave!")
.setContentText("Your PrePark spot has expired, time to go home!")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pIntent)
.setAutoCancel(true)
.build();
notificationManager.notify(0, n);
}
else{
Notification n = new Notification.Builder(this)
.setContentTitle("Ya got 5 minutes left in your PrePark spot!")
.setContentText("Better get going soon here")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pIntent)
.setAutoCancel(true)
.build();
notificationManager.notify(0, n);
}
}
主な活動
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.os.Bundle;
import android.widget.TextView;
public class Countdown extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_countdown);
LocalBroadcastManager.getInstance(this).registerReceiver(
new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
TextView textView= findViewById(R.id.t1);
int timeLeft = intent.getIntExtra("timeSent", 0);
if(timeLeft>0) {
textView.setText("You have " + timeLeft + " minutes left");
}
else{
textView.setText("Y'all outta time, see ya again soon!");
}
}
}, new IntentFilter(CountdownService.ACTION_LOCATION_BROADCAST)
);
}
@Override
protected void onResume() {
super.onResume();
startService(new Intent(this, CountdownService.class));
}
@Override
protected void onPause() {
super.onPause();
stopService(new Intent(this, CountdownService.class));
}
}
XML
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center">
<TextView
android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerInParent="true"
android:textSize="32sp"
/>
</RelativeLayout>
オハイ、私がstartServiceをonResumeから取り出しても問題ないでしょうか?または、私の主なアクティビティonCreateにstartServiceも追加する必要がありますか?助けてくれてありがとう! –
あなたの必要性に関連しています。アクティビティが開始された後にサービスを開始する場合は、onCreateメソッドでstartServiceを呼び出します。 そして、アプリケーションがバックグラウンドになるときにサービスを停止したくない場合は、onPauseメソッドからstopServiceを削除してください。 – Reza
本当にありがとうございました、私はそれを働かせました。 –