我正在尝试在我的应用中使用NotificationCompat.Builder类。以下代码在ICS手机上运行得非常好,但不是姜饼。根据我的理解,支持库是否允许从低至API级别4的手机访问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());
}
}
}
答案 0 :(得分:8)
您必须提供在单击通知时将触发的Intent。否则,通知将不会显示在Gingerbread上。 如果您不想在单击通知时启动活动,则只需传递一个空的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