1

我正在尝试在我的应用程序中使用 NotificationCompat.Builder 类。以下代码在 ICS 手机上运行得非常好,但在 Gingerbread 上却不行。据我了解,支持库是否允许从 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());
        }
    }
}
4

1 回答 1

8

您必须提供单击通知时将触发的 Intent。否则,通知将不会显示在 Gingerbread 上。如果您不想在单击通知时启动 Activity,则可以传递一个空的 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
于 2013-06-19T06:36:11.193 回答