0

I'm try to make an android exercise of my college and I'm stuck on the alarm part. When you get the time determined by the alarm, I need to send an alert and can open another activity for the notification.

But as the class is already extending the BroadcastReceiver, how can I start another acitivity?

My class are like this:

public class ReceiverAlarme extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    //inicia a Activity do Treino
    Intent itTreino = new Intent(this, telaTreino.class);

    //Does not work
    //startActivity(itTreino);

    //tentei fazer uma notificação que abrisse o programa, mas não rolou
    Notification.Builder notificacao = new Notification.Builder(this);
    notificacao.setDefaults(Notification.DEFAULT_ALL)
            .setSmallIcon(android.R.drawable.ic_dialog_email)
            .setContentTitle("Agendamento do teste")
            .setContentText("Você tem um teste agendado!")
         .setContentIntent(PendingIntent.getActivities(getApplicationContext(), 1, itTreino, PendingIntent.FLAG_CANCEL_CURRENT));

    NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    nm.notify(1, notificacao.build());
}
}
Pooya
  • 6,083
  • 3
  • 23
  • 43
lucianosds
  • 11
  • 1
  • I would recommend that you also give the user the option of sticking with the `Notification`, perhaps using `setPriority()`. Not all users will necessarily want their alarm clock app to take over the foreground, particularly if the device is in use. – CommonsWare Apr 23 '16 at 18:34

1 Answers1

2

You need to run startActivity from a Context so try the following:

context.startActivity(itTreino);
Pooya
  • 6,083
  • 3
  • 23
  • 43
  • 3
    Note that this will require `Intent.FLAG_ACTIVITY_NEW_TASK` on the `Intent`, since `startActivity()` is not being called from an activity. – CommonsWare Apr 23 '16 at 18:34