在状态栏上单击通知图标时启动活动?

时间:2012-02-27 18:10:45

标签: android android-activity android-service android-notifications

我正在使用闹钟管理器来设置闹钟。当它关闭时,接收器启动此服务。从该服务使用以下代码进行通知。我想在点击通知时运行一个活动,但是当我点击通知图标时没有任何反应。

它只是显示这个圆圈在移动,好像有什么东西在加载,就是这样。

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

}

2 个答案:

答案 0 :(得分:1)

嘿,我清理完项目后突然工作了......

答案 1 :(得分:0)

我查看了您的通知代码。一切看起来都是正确的。

你是什么意思“但是当我点击通知图标时才会发生。这只是显示这个圆圈移动,好像有什么东西在加载,就是这样”。我问,因为“它只显示这个圈子”肯定意味着它正在做某事。