如何在android中创建通知?

时间:2015-12-20 13:46:59

标签: android

我正在尝试在android中创建简单通知。目的是在发生某些事情时在手机屏幕顶部显示一个小图标。然后我可以点击通知,该通知将启动应用程序或将其置于前台。

仅出于测试目的,我正在关注this难以理解且不是简单的示例,并且还找到了this条目以获取第一个错误。

无论如何这里是代码:

    Notification mBuilder =
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.icon1)
            .setContentTitle("My notification")
            .setContentText("Hello World!");
    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(this, MainActivity.class);

    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(MainActivity.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                0,
                PendingIntent.FLAG_UPDATE_CURRENT
            );
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    // mId allows you to update the notification later on.
    int mId = 1001;
    mNotificationManager.notify(mId, mBuilder.build());

以下是我要导入的内容:

...
import android.app.Notification;
import android.app.NotificationManager;
....
import android.support.v7.app.ActionBar;
//import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.NotificationCompat;
import android.support.v7.widget.Toolbar;

目前的错误是

Error:(117, 32) error: incompatible types
required: Notification
found:    Builder

如何解决这个问题?

4 个答案:

答案 0 :(得分:3)

更改

Notification mBuilder 

NotificationCompat.Builder mBuilder 

由于您要实例化new NotificationCompat.Builder(this),因此无法将其分配给Notification对象

答案 1 :(得分:1)

我在Android上的Udacity教程后遇到了同样的问题,发现我错过了演员。您应该将代码更改为

       NotificationCompat.Builder mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(getContext())
                            .setSmallIcon(R.drawable.icon1)
                            .setContentTitle(My notification")
                            .setContentText("Hello World!");

您正在使用NotificationCompat而您正在分配通知

答案 2 :(得分:1)

继续@Blackbelt的回答,android.support.v7.app.NotificationCompat.Builderandroid.support.v4.app.NotificationCompat.Builder的子类,其成员函数如setSmallIcon(R.drawable.icon1)返回android.support.v4.app.NotificationCompat.Builder的实例,无法转换为{{1} }}。因此,最佳做法是:

android.support.v7.app.NotificationCompat.Builder

答案 3 :(得分:0)

使用以下代码片段,它可用于在android

中创建通知
import android.app.NotificationManager;
import android.support.v4.app.NotificationCompat;

//build your notification here.
                NotificationCompat.Builder mBuilder =
                        new NotificationCompat.Builder(getContext())
                                .setSmallIcon(R.drawable.icon1)
                                .setContentTitle("My notification")
                                .setContentText("Hello World!")
                                .setAutoCancel(true); //notification will be removed when once you enter application.

希望这可以帮助你..

干杯