如何通过广播接收器启动活动

时间:2016-04-23 18:24:07

标签: java android broadcastreceiver

我正在尝试对我的大学进行机器人练习,而且我被困在警报部分。当您获得由警报确定的时间时,我需要发送警报并可以打开另一个通知活动。

但是由于该类已经在扩展BroadcastReceiver,我该如何开始另一个活动?

我的班级是这样的:

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());
}
}

1 个答案:

答案 0 :(得分:2)

您需要从startActivity运行Context,请尝试以下操作:

context.startActivity(itTreino);