Android通知给Notification类的错误

时间:2013-01-29 06:57:26

标签: android eclipse notifications stackmob

我正在为Android创建一个小应用程序来通知。

但是它在Notification类错误中给出了错误(11或16支持的API级别)。 然后我尝试使用NotificationCompat类,但它显示在我import package import android.support.v4.app.NotificationCompat.Builder;

时无法将资源解析为某种类型

换句话说,如果我使用Notification类,那么它会给出API级错误,如果我使用NotificationCompat,那么它会给出该资源错误。如何解决这两个错误?

 public void createNotification(View view) {
    // Prepare intent which is triggered if the
    // notification is selected
    Intent intent = new Intent(this, NotificationReceiverActivity.class);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
    // Context context=new Context();
    // Build notification
    // Actions are just fake
     NotificationCompat.Builder noti = new NotificationCompat.Builder(this)
    .setContentTitle("New mail from " + "star.ankit90@gmail.com")
    .setContentText("Subject").setSmallIcon(R.drawable.ic_launcher)
    .setContentIntent(pIntent)
    .addAction(R.drawable.ic_launcher, "Call", pIntent)
    .addAction(R.drawable.ic_launcher, "More", pIntent)
    .addAction(R.drawable.ic_launcher, "And more", pIntent).build();
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    // Hide the notification after its selected
    noti.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(0, noti);
}

1 个答案:

答案 0 :(得分:1)

我使用您的代码时遇到了编译错误。

此固定代码成功编译:

// Prepare intent which is triggered if the
    // notification is selected
    Intent intent = new Intent(this, NotificationReceiverActivity.class);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

    // Build notification
    // Actions are just fake
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

    builder.setContentTitle("New mail from " + "star.ankit90@gmail.com")
    .setContentText("Subject").setSmallIcon(R.drawable.ic_launcher)
    .setContentIntent(pIntent)
    .addAction(R.drawable.ic_launcher, "Call", pIntent)
    .addAction(R.drawable.ic_launcher, "More", pIntent)
    .addAction(R.drawable.ic_launcher, "And more", pIntent).build();
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    // Hide the notification after its selected
    Notification notification = builder.build();

    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(0, notification);

也许它会对你有用。如果不是堆栈跟踪可以帮助。