在通知图标单击上启动新活动

时间:2013-06-21 07:42:30

标签: android

我已经可以在通知栏中显示一个图标,如下面的代码所示。我想在用户点击此图标时启动新活动(myclass),但我不知道如何执行此操作。我应该在哪里放置意图?

public class NotificationActivity extends Activity {
    AlarmManager am;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification);
        am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        setRepeatingAlarm();
    }

    public void setRepeatingAlarm() {
        Intent intent = new Intent(this, TimeAlarm.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
            intent, PendingIntent.FLAG_CANCEL_CURRENT);
        am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
            (20 * 1000), pendingIntent);
        System.out.println("Calling Alaram...");
    }
}



public class BootUpReciever extends BroadcastReceiver {  
    NotificationManager nm;

    @Override  
    public void onReceive(Context context, Intent intent) {
        System.out.println("in broad....");

        nm = (NotificationManager) context  .getSystemService(Context.NOTIFICATION_SERVICE);
        CharSequence from = "Lokesh";
        CharSequence message = "Notification Test...";
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            new Intent(), 0);
        Notification notif = new Notification(R.drawable.cherry_icon,
            "Notification Test...", System.currentTimeMillis());
        notif.setLatestEventInfo(context, from, message, contentIntent);
        nm.notify(1, notif);

        if ((intent.getAction() != null) &&  
                (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")))
        {
            System.out.println("in broadcast receiver.....");
            Intent i = new Intent(context, MainActivity.class);  
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
            context.startActivity(i);  
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您提供给“通知”构建器的待处理意图应包含一个意图,该意图将在用户单击通知时启动活动。您需要创建一个意图来启动您的一项活动:

Intent resultIntent = new Intent(this, ResultActivity.class);

然后使用此意图创建pendingIntent:

PendingIntent resultPendingIntent =
        stackBuilder.getPendingIntent(
            0,
            PendingIntent.FLAG_UPDATE_CURRENT
        );

然后使用它创建通知。当用户点击您的通知时,系统会启动ResultActivity

整个代码段:

NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Hello World!");

// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.class);

// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ResultActivity.class);

// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
        stackBuilder.getPendingIntent(
            0,
            PendingIntent.FLAG_UPDATE_CURRENT
        );
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());

阅读开发人员文档中的详细文章,以获得一个很好的解释: https://developer.android.com/guide/topics/ui/notifiers/notifications.html