0

I know there are many threads on Stackoverflow on this topic, and I have read each and every single one, but I am still very confused and am very sorry but I feel like I should post this.

When the application is run nothing really happens with the receivers, as if they are not receiving anything

My service:

    @Override
public int onStartCommand(Intent intent, int flags, int startId) {

    int timerValue = pv.getTimerValues();
    aCounter = new CountDownTimer((timerValue), 1000) { // set to
                                                                    // timerValue
                                                                    // later
        public void onTick(long millisUntilFinished) {
            int minutes, seconds;   
            String sSeconds, sMinutes, toSend;
            seconds = (int) (millisUntilFinished / 1000) % 60;
                minutes = (int) ((millisUntilFinished / (1000 * 60)) % 60);
            sSeconds = Integer.toString(seconds);
            sMinutes = Integer.toString(minutes);
            toSend = (sMinutes + ":" + sSeconds);
            sendMessage(toSend);
            countDownCheck();
        }

        public void onFinish() {
            MediaPlayer dingeffect = MediaPlayer.create(
                    getApplicationContext(), R.raw.timerfinished);
            // mPlayer0.stop();
            try {
                dingeffect.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            dingeffect.start();
        //  Intent intent2 = new Intent(ServiceTest.this, Meditate.class);
            //startActivity(intent2);
            sendMessage("finished");
        }
    };
    aCounter.start();
    return START_NOT_STICKY;
}

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

private void sendMessage(String send) {
    Intent intent1 = new Intent("countdowntimer");
    // You can also include some extra data.
    intent1.putExtra("message1", send);
    sendBroadcast(intent1);
}

My receiving activity: Inside the onCreate:

registerReceiver(mMessageReceiver1, new IntentFilter("countdowntimer"));
final Intent countDownService = new Intent(Meditate2.this,
            ServiceTest.class);

startService(countDownService);

and my broadcastreceiver inside the same activity

    private BroadcastReceiver mMessageReceiver1 = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        String message = intent.getStringExtra("message1");
            setTimer(message);

    }
};

In the manifest I have declared the service as such:

 <service android:name=".ServiceTest"></service>

The weird thing is, it was working then after I fixed some other stuff this stopped working, so I really have no clue why it is not. Help please!

2 Answers2

0

Your broadcast receiver should know what intents to listen to. Register your receiver with an intentFilter that listens to your countdowntimer intent like this:

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("countdowntimer");
activity.registerReceiver(this, intentFilter);
Aster
  • 809
  • 2
  • 8
  • 13
0

I will tell you what is different between your code and my (working) code. In the activity onCreate:

LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver1, new IntentFilter("countdowntimer"));

In the activity onDestroy:

LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver1);

In sendMessage:

LocalBroadcastManager.getInstance(context).sendBroadcast(intent);

I don't know if this makes a lot of difference, but I used LocalBroadcastManager.getInstance.