1

我有一个创建通知的类。类是这样的:Onclick通知,在Android中启动一个活动

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


    } 
    } 

我有一个名为活动:QuizScreen,这是需要被点击的通知时被打开。我已经尝试使用:

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

但我收到此错误:

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

我要去哪里错了?我应该如何解决这个问题?

回答

3

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

广播接收器不算作上下文吗? 使用来自的onReceive上下文参数(上下文的背景下,意图意图),而不是

+0

如果你不介意,可以请你提出一些修改在发布的代码? – kittu88

+0

它与Ankit刚才写的 –

1

试试下面的代码开始活动

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

基本上是以尼克拉斯建议,接收机犯规算作背景下,这样你们会需要使用上下文u进入onReceive方法,或者像context.getApplicationContext一样使用它。

0

使用使用Intent quiz_intent = new Intent(context, QuizScreen.class);代替Intent quiz_intent = new Intent(TimeAlarm.this, QuizScreen.class);例如:代替 Intent quiz_intent = new Intent(context, QuizScreen.class);Intent quiz_intent = new Intent(TimeAlarm.this, QuizScreen.class);

例如:

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);` 
+0

相同,你不认为自己写答案。如果有人解决了你的查询,只需接受! – AAnkit