简体   繁体   中英

Launching an activity when a notification icon is clicked on the status bar?

I'm using the alarm manager to set an alarm. When it goes off the receiver starts this service. From that service a notification is made using the following code. I want to run an activity when the notification is clicked, but when i click the notification icon nothing happens.

It just shows this circle moving as if something is loading and that's it.

package com.rythmal.alarmtest;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;

public class AlarmProcess extends Service {

    private static final int MY_NOTIFICATION_ID = 1;
    private NotificationManager mNotificationManager;
    private Notification myNotification;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        String ns = Context.NOTIFICATION_SERVICE;
        mNotificationManager = (NotificationManager) getSystemService(ns);

        int icon = R.drawable.ht_icon;
        CharSequence tickerText = "Rythmal";
        long when = System.currentTimeMillis();

        myNotification = new Notification(icon, tickerText, when);

        Context context = getApplicationContext();
        CharSequence notificationTitle = "My notification";
        CharSequence notificationText = "Hello World!";
        Intent notificationIntent = new Intent(this,com.rythmal.alarmtest.ResultRun.class);


        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        PendingIntent pendingIntent = PendingIntent.getActivity(getBaseContext(), 0, notificationIntent, 0);

        myNotification.defaults |= Notification.DEFAULT_SOUND;
        myNotification.flags |= Notification.FLAG_AUTO_CANCEL;

        myNotification.setLatestEventInfo(context, notificationTitle,notificationText, pendingIntent);
        mNotificationManager.notify(MY_NOTIFICATION_ID, myNotification);

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

}

嘿,在我清理完项目后,它一下子就起作用了。

I have looked over your Notification code. Everything looks correct.

What do you mean by "but when i click the notification icon nothin happens.it just shows this circle moving as if something is loading and thats it". I ask, because "it just shows this circle" definitely implies that it is doing something.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM