2

I have the following code to post intents:

        alarmMgr = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent("myLog");
        intent.putExtra("message", new Date().toString());
        alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
        alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME,System.currentTimeMillis()+1000,5000,alarmIntent);

And the following receiver:

public class myReceiver extends BroadcastReceiver {
    @Override public void onReceive(Context context, Intent intent) {
        // Extract data included in the Intent
        String message = intent.getStringExtra("message");
        logText.setText(logText.getText()+"\n"+message);
    }
}
private BroadcastReceiver mMessageReceiver = new myReceiver();

Why my mMessageReceiver fires only once with the sendBroadcast, but does not receive the intents broadcasted by my alarmMgr?

EDIT: Also this is the related part of my AndroidManifest.xml file:

    <receiver android:name=".MainApp$myReceiver" >
        <intent-filter>
            <action android:name="com.example.*.*" />
        </intent-filter>
    </receiver>

EDIT 2: More info:

@Override public void onResume() {
    super.onResume();
    // Register mMessageReceiver to receive messages.
    IntentFilter myIntentFilter = new IntentFilter();
    myIntentFilter.addAction("myLog");
    LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
            myIntentFilter);
}

This overrides MainActivities onResume().

jazzcool
  • 113
  • 2
  • 10

1 Answers1

2

Hmm I might be out of my depth here, but this works for me. So in this case, I am setting a alarm (like 10 mins or something) ahead of the current time.

    // Calculate the time when it expires.
    long wakeupTime = System.currentTimeMillis() + duration;

    Intent myIntent = new Intent(MainActivity.this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);

    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, wakeupTime, pendingIntent);

This sets an alarm to go off and to wake up my BroadcastReceiver (AlarmReceiver). I hope this helps and I am not way off base here.

Daedalus
  • 255
  • 1
  • 2
  • 9
  • Yes I've seen similar code. This link: http://stackoverflow.com/questions/4660823/android-alarm-not-working is very good at explaining. When I copy paste that code it works, but I'm just trying to figure out what I'm doing wrong.. – jazzcool Aug 05 '15 at 17:34
  • could be because of this line `Intent intent = new Intent("myLog");` Your stuffing the intent with a String when you should be stuffing it with the Activity you are launching it from and the Receiver you want to receive it with. Like so: `Intent myIntent = new Intent(MainActivity.this, AlarmReceiver.class);` – Daedalus Aug 05 '15 at 17:43
  • No I'm pretty sure that is viable. That string is called "Action", and you should be able to set your broadcast receiver to listen to specific actions. At edit2 you can see that. But yeah maybe I'm not setting the registerReceiver right... – jazzcool Aug 05 '15 at 17:58
  • hmm when i send out a broadcast with an intent i do it like `Intent newIntent = new Intent("com.miproducts.miwatch"); newIntent.putExtra("MiWatch", wakeupTime); newIntent.setAction("com.miproducts.miwatch"); newIntent.setPackage("com.miproducts.miwatch"); sendBroadcast(newIntent);` I guess there is many ways to do it. – Daedalus Aug 05 '15 at 18:05