2010-06-13 75 views
1

我试图完成的是通过通知管理器 发送一个通知,一旦点击就会在应用程序中执行某些操作,只有当它正在运行时。 我曾尝试使用:PendingIntent从一个通知发送

notification.contentIntent = PendingIntent.getActivity(this, nNotificationCounter, Someintent, PendingIntent.FLAG_NO_CREATE) 

哪个八方通导致异常一次尝试使用通知。 我切换到:

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

RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.some_notification); 
contentView.setTextViewText(R.id.title, sTitle); 
contentView.setTextViewText(R.id.text, sText); 
notification.contentView = contentView; 
notification.defaults |= Notification.DEFAULT_SOUND; 
notification.number = nNotificationCounter; 

Intent notificationIntent = new Intent(this, MainWindow.class).setAction(ACTION_RESET_MESSGE_COUNTER); 
notification.contentIntent = PendingIntent.getBroadcast(this, nNotificationCounter, notificationIntent, 0); 

,虽然这个代码不会导致异常。它不叫我的广播接收器,其定义如下:

public class IncomingReceiver extends BroadcastReceiver { 
      @Override 
      public void onReceive(Context context, Intent intent) { 
       if (intent.getAction().equals(ACTION_RESET_MESSGE_COUNTER)) { 
        System.out.println("GOT THE INTENT"); 
        return; 
       } 

      } 
     } 

,并在OnCreate设置:

IntentFilter filter = new IntentFilter(ACTION_RESET_MESSGE_COUNTER); 
     IncomingReceiver receiver = new IncomingReceiver(); 
     context.registerReceiver(receiver, filter); 

有谁看到一些错误的代码? 或者当通知被点击时我将如何获取消息,但是如果尚未创建任何活动,则不会创建任何活动。

编辑:添加了意图创建和通知创建。

感谢, 汤姆

+0

确保'notificationIntent'设置正确。既然你没有在这里包括它的代码,我们不能提供很多帮助。 – CommonsWare 2010-06-13 15:36:04

+0

我添加了所有的代码,通常是这种方式来获得一个事件,只有当通知运行acticity? – totem 2010-06-13 15:45:56

回答

0

你最有可能没有得到广播,因为你是正确创建IntentIntent(Context, Class<?>)告诉系统创建一个“创建”该类的意图。换句话说,这个意图直接仅限于硬编码类。只需使用new Intent(String)或在您的特定情况下new Intent(ACTION_RESET_MESSGE_COUNTER),您将创建我称之为“广播”意图或文档称为“操作”意图的内容。这样,意图将针对正在倾听特定广播/动作的任何人。