2

私はAndroidが初めてなので、助けが必要です。新しい Notification.Builder インスタンスを構築する方法を理解できないようです。悲しいことに、私がフォローしているチュートリアルでは、古いコンストラクターを使用しています。これは、Android リファレンス ドキュメントを熟読した後、明確に述べられています。押すと簡単な通知が表示されるボタンを作成しました。他の例を見つけた後、すべてのコードは問題ないように見えますが、ビルド メソッドの下で Eclipse で赤い線のエラーが発生します。どんな助けでも大歓迎です:

Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

    Context context = Main.this;
    Intent intent = new Intent(context, Main.class);
    PendingIntent pending = PendingIntent.getActivity(Main.this, 0, intent, 0);

    Notification noti = new Notification.Builder(Main.this)
    .setTicker("This is important!")
        .setContentTitle("PLEASE READ!")
        .setContentText("Important message from me!")
        .setSmallIcon(android.R.drawable.stat_notify_more)
        .setWhen(System.currentTimeMillis())
        .setContentIntent(pending)
        .build();


    NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    nm.notify(0, noti);
    }
});
4

2 に答える 2

1

Notification.Builder.build()は API レベル 16 です。ターゲットを v16 に設定していないか、v16 Android 開発キットをダウンロードしていない可能性があります。そのため、Eclipse はそのメソッドを認識せず、無効としてフラグを立てます。

Notification.Builderのドキュメントをご覧ください。

于 2012-10-22T20:03:13.543 に答える
0

android.support.v4 を使用している場合は、Android v4 サポートにある NotificationCompat を使用します。

于 2012-10-31T14:11:09.913 に答える