如何在Android api级别19上显示通知

时间:2014-10-10 07:00:27

标签: android

public void onClick(View v) {
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 1,
                notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        NotificationManager nm = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);

        Resources res = this.getResources();
        Notification.Builder builder = new Notification.Builder(this);

        builder.setContentIntent(contentIntent)
                .setSmallIcon(R.drawable.ic_launcher)
                .setLargeIcon(
                        BitmapFactory.decodeResource(res,
                                R.drawable.ic_launcher)).setTicker("helllo")
                .setWhen(System.currentTimeMillis()).setAutoCancel(true)
                .setContentTitle("Messge").setContentText("Messge22");
        Notification n = builder.build();

        nm.notify(5, n);
    }

我正在尝试使用此代码发送通知按钮单击但是代码中出现错误它显示错误调用需要API级别11(当前最小值为8):android.app.Notification.Builder #setContentIntent我已设置项目Api等级19请帮助我在哪里做错了请建议我解决方案目标= android-19项目属性,而我必须代码:

  <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

1 个答案:

答案 0 :(得分:1)

如果您的应用支持{4}的Android版本,则可以使用Android Support library中提供的NotificationCompat.Builder

Notification.Builder - 此类已添加到Android 3.0 Honeycomb [API 11]中。因此,如果您需要支持较旧的SDK,则需要使用NotificationCompact

NotificationCompat.Builder - 此类版本为4 Support Library(兼容Android 1.6及更高版本)。

有关详细信息,请转到How exactly to use Notification.Builder