kittu88
kittu88

Reputation: 2461

Onclick notification, start an activity in Android

I have a class for creating notifications. The class is like this:

public class TimeAlarm extends BroadcastReceiver {

     NotificationManager nm;

     @Override
     public void onReceive(Context context, Intent intent) {
      nm = (NotificationManager) context
        .getSystemService(Context.NOTIFICATION_SERVICE);
      CharSequence from = "Scheduled Training";
      CharSequence message = "Please attend the scheduled training";
      //Intent notifyIntent = new Intent();



      PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);

      Notification notif = new Notification(R.drawable.ic_launcher,
        "NeuroQ Scheduled Training", System.currentTimeMillis());
      notif.setLatestEventInfo(context, from, message, contentIntent);
      nm.notify(1, notif);


     }
    }

I have an activity called: QuizScreen, which is required to be opened when the notification is clicked. I have tried using:

Intent quiz_intent = new Intent(TimeAlarm.this, QuizScreen.class);
                        TimeAlarm.this.startActivity(quiz_intent);

But I am getting this error:

The constructor Intent(TimeAlarm, Class<QuizScreen>) is undefined

Where am I going wrong? How should I solve this issue?

Upvotes: 1

Views: 8143

Answers (4)

saravana
saravana

Reputation: 564

use : Intent quiz_intent = new Intent(context, QuizScreen.class); instead of Intent quiz_intent = new Intent(TimeAlarm.this, QuizScreen.class);

For example:

public class MyBroadcastReceiver extends BroadcastReceiver {

private NotificationManager mNotificationManager;
private int SIMPLE_NOTFICATION_ID;

@Override
public void onReceive(Context context, Intent intent) {

    mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notifyDetails = new Notification(R.drawable.android,"Time Reset!",System.currentTimeMillis());
    PendingIntent myIntent = PendingIntent.getActivity(context, 0, new Intent(context, Second.class), 0);
    notifyDetails.setLatestEventInfo(context, "Time changed", "Click on me to view my second activity", myIntent);
    notifyDetails.flags |= Notification.FLAG_AUTO_CANCEL;

    mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);`

Upvotes: 0

user1991618
user1991618

Reputation:

use Intent quiz_intent = new Intent(context, QuizScreen.class); instead of Intent quiz_intent = new Intent(TimeAlarm.this, QuizScreen.class); For example,

public class MyBroadcastReceiver extends BroadcastReceiver {
  private NotificationManager mNotificationManager;
  private int SIMPLE_NOTFICATION_ID;

  @Override
  public void onReceive(Context context, Intent intent) {
    mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notifyDetails = new Notification(R.drawable.android,"Time Reset!",System.currentTimeMillis());
    PendingIntent myIntent = PendingIntent.getActivity(context, 0, new Intent(context, Second.class), 0);
    notifyDetails.setLatestEventInfo(context, "Time changed", "Click on me to view my second activity", myIntent);
    notifyDetails.flags |= Notification.FLAG_AUTO_CANCEL;

    mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
  }
}

Upvotes: 3

AAnkit
AAnkit

Reputation: 27549

try below code to start activity

Intent quiz_intent = new Intent(context, QuizScreen.class);
                        TimeAlarm.this.startActivity(quiz_intent);

Basically as Nicklas suggested, Receiver doesnt count as context, so u ll need to use context u getting in onReceive method, or use it like context.getApplicationContext.

Upvotes: 1

Nicklas Gnejs Eriksson
Nicklas Gnejs Eriksson

Reputation: 3415

The broadcast receiver does not count as a context? Use the context parameter from the onReceive(Context context, Intent intent) instead

Upvotes: 1

Related Questions