2012-11-05 53 views
1

我试图在我的应用程序中使用NotificationCompat.Builder类。以下代码在ICS电话上运行得非常好,但不是姜饼。根据我的理解,支持库是否允许从低至API级别4的手机访问Builder?我在做一些根本性错误?NotificationCompat.Builder不工作在姜饼

public class MainActivity extends Activity implements OnClickListener { 
NotificationCompat.Builder builder; 
NotificationManager nm; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     builder = new NotificationCompat.Builder(this); 
     nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

     builder.setContentTitle("Test Notification") 
     .setContentText("This is just a test notification") 
     .setTicker("you have been notified") 
     .setSmallIcon(R.drawable.ic_stat_example) 
     .setWhen(System.currentTimeMillis()); 

     findViewById(R.id.btn_notify).setOnClickListener(this); 
    } 

    public void onClick(View arg0) { 
     // TODO Auto-generated method stub 
     if(arg0.getId() == R.id.btn_notify){ 
     nm.notify(1, builder.build()); 
     } 
    } 
} 

回答

8

您必须提供在点击通知时将触发的意图。否则,通知将不会显示在姜饼上。 如果您不想在单击通知时启动活动,则可以按照以下方式传递空的Intent。

Intent intent = new Intent(); 
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK); 

builder.setContentTitle("Test Notification") 
    .setContentText("This is just a test notification") 
    .setTicker("you have been notified") 
    .setSmallIcon(R.drawable.ic_stat_example) 
    .setWhen(System.currentTimeMillis()) 
    .setContentIntent(pendingIntent); // add this 
+0

请加一些说明 –

+0

当然!解释补充说。 –

+0

我被困了一段时间,为什么我的通知突然停止在较旧的API中工作,事实证明我不仅需要该意图,而且还需要我在转换中丢弃的股票信息。谢谢。 – LoungeKatt