1

I want to throw an alarm intent after certain minutes. From this intent some data is received and a view from the app shoud show it.

This code is in the activity

public class MainActivity extends Activity{

    DatePicker pickerDate;
    TimePicker pickerTime;
    Button buttonSetAlarm;
    TextView info;

...
private void setAlarm(){
    Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+1000, pendingIntent);   
}

Then the broadcast receiver, in the Activity class or outside in a separated java file ( see end of post ) , looks like this:

public class AlarmReceiver extends BroadcastReceiver {

    private static final int MY_NOTIFICATION_ID=1;
    NotificationManager notificationManager;
    Notification myNotification;

    @Override
    public void onReceive(Context context, Intent intent) {
          Toast.makeText(context, "Alarm received!", Toast.LENGTH_LONG).show();
          String information = getInformation();
          info.setText("this is your info"+information); <--------------------
    }
}

How can this be done ?

I've tryed to get the view from the context by inflating , that was unsuccesfull

following android - How to get view from context? and

I've also tryed to get the view from a bundle inside the intent but it seems that only strings and serializable objects can be passed by them.

If i Inner the broadcastreceiver in the activity , the alarm is not thrown ( No toast message appears, no changes in the textview ) described in : Calling SetContentView() from broadcast receiver althought it seems that this solutions don't work as said in : BroadcastReceiver as inner class

Thank you in advance !

L.

Community
  • 1
  • 1
Lau Llobet
  • 577
  • 7
  • 21

2 Answers2

1

The solution was to instance a broadcast receiver on the constructor of the class and register it programatically.

The thing is that it will stop working when paused, but anyway I find absurd to change a text view of an application that is destroyed.

If i would like to show alarms even if the app is destroyed i would use a broadcast in its own .java file, outside the activity, and registered in the manifest that would re-launch the app if required, then inside the recently opened app refresh it's content.

      Commig from the constructor ...

            br = new BroadcastReceiver() {
                private static final int MY_NOTIFICATION_ID=1;
                NotificationManager notificationManager;
                Notification myNotification;

                @Override
                public void onReceive(Context context, Intent intent) {
                    Toast.makeText(context, "Alarm received!", Toast.LENGTH_LONG).show();

                    info.setText("DONE!");
                }
        };

    }

    @Override
    public void onResume(){
        super.onResume();
        registerReceiver(br, new IntentFilter("onalarm"));   
    }

    @Override
    public void onPause(){
        unregisterReceiver(br);
        super.onPause();
    }

    private void setAlarm(){
        info.setText("cuasimodo");
        //Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
        Intent intent = new Intent("onalarm");
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
        AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+1000, pendingIntent);    
    }
Lau Llobet
  • 577
  • 7
  • 21
  • Yes, you can do this. If you want to do this when the app is not running, you will need to launch the app from `onReceive()`. You can pass some extra in the `Intent` that tells the app that it is being launched from the alarm, so that the app can do something intelligent. – David Wasser Nov 20 '13 at 11:11
1

You cannot access UI components from outside an activity. To do what you want you will need to have the onReceive() method call a method on the activity itself, which would then do the work.

In general this is the wrong approach. You probably don't need to use AlarmManager and a BroadcastReceiver to accomplish this. You should just create a Handler in your activity and post a Runnable or a message to your Handler. You can post something so that it will get executed (Runnable) or dispatched (message) at some time in the future.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • Thank you David, your answer looks clear to me. For the moment i'm buliding a prototype that will need AlarmManager in the future , this requirement of changing a view from an alarm is only for debugging purposes. That's why I find my answer more correct for my case but i check your one as the correct one, could you take a look to the solutions i've commented? It's only to know your opinion, specially on if re-launching an app from a broadcast receiver is a posible/elegant solution. Thank you for your help. – Lau Llobet Nov 20 '13 at 10:56
  • Here there's a tutorial explaining how to do David Wasser proposal : http://robotoworks.com/2012/11/exploring-android-handlers/ – Lau Llobet Nov 21 '13 at 11:26