无法在我的BroadcastReceiver中获得额外内容

时间:2017-08-18 12:46:42

标签: android service foreground

我在实现服务类时遇到了问题。通过日志一切正常,服务(额外的工作和倒计时工作,并在时间结束后显示通知)。但我不能得到额外的东西,以更新我的观点。我的想法是在应用程序被杀死时让我的服务运行,并在回收站视图中更新多个计时器。 这是我的代码。任何信息都会有所帮助

我正在尝试注册我的服务的部分

Intent startServiceIntent = new Intent(context, TimerService.class);
        startServiceIntent.putExtra(EXTRAS_TIME, timeCountInMilliSeconds);
        context.startService(startServiceIntent);
        Log.d("Service", "startService");

        broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                long millisUntilFinished = intent.getLongExtra(EXTRAS_COUNTDOWN, 0L);

                Log.d("Service", "Smthisreceived");

                task.timeLeft = String.valueOf(timeLeft); // here add left time for timer

                textViewTimeCV.setText(hmsTimeFormatter(millisUntilFinished)); // just handling status bar values

                progressBarCV.setProgress((int) (millisUntilFinished / 1000));
            }
        };

        context.registerReceiver(broadcastReceiver, new IntentFilter("android.intent.category.DEFAULT"));
        Log.d("Service", "receiverRegistered");

服务

public class TimerService extends Service {

private CountDownTimer ctd;
private Intent broadcastIntent;
private int requestCode;
private long timeCountInMillis;

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d("In service", "+1");

    Intent notificationIntent = new Intent(this, MainActivity.class);
    requestCode =  (int)(intent.getLongExtra(EXTRAS_TIME, 0L));

    timeCountInMillis = intent.getLongExtra(EXTRAS_TIME, 0L);

    broadcastIntent = new Intent(COUNTDOWN_BROADCAST_RECEIVER);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, notificationIntent, 0);

    Notification.Builder notification = new Notification.Builder(this)
            .setContentTitle(getTextFromResource(this, R.string.app_name))
            .setContentText(getTextFromResource(this, R.string.your_timer) + " "
                    + getTextFromResource(this, R.string.is_currently_running))
            .setSmallIcon(ic_launcher)
            .setContentIntent(pendingIntent);

    startForeground(requestCode, notification.build());

    ctd = new CountDownTimer(timeCountInMillis , 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            broadcastIntent.putExtra(EXTRAS_COUNTDOWN, millisUntilFinished);
            Log.d("Extras_are_put", String.valueOf(millisUntilFinished));
        }

        @Override
        public void onFinish() {
            stopForeground(true);

        }
    }.start();

    return START_STICKY;
}}

我的常数

public static final String COUNTDOWN_BROADCAST_RECEIVER = "com.example.hp.intimer.TimerService";
public static final String EXTRAS_COUNTDOWN = "Countdown";
public static final String EXTRAS_TIME = "Time";

0 个答案:

没有答案