1

通知用のアンドロイド用の小さなアプリを作成しています。

ただし、通知クラス エラーでエラーが発生します (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);
}
4

1 に答える 1

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);

多分それはあなたのために働くでしょう。そうでない場合は、スタックトレースが役立ちます。

于 2013-01-29T07:20:54.517 に答える