Android Studio预期的类或包错误

时间:2016-03-31 21:24:10

标签: android android-studio alarmmanager android-notifications

我无法弄清楚为什么会发生此错误。我正在创建一个警报按钮,向用户发送VIA振动通知。

" new NotificationCompat()"是我的错误。

这是代码:



public class AlertReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {

        createNotification(context, "It Is Time To Stand Up!","20 Minutes Have Passed", "Alert");

    }

    public void   createNotification(Context context, String msg, String msgText, String msgAlert ) {

        PendingIntent notificIntent = PendingIntent.getActivity(context,0,new Intent(context,CustomizeAlertsPage.class),0);

        NotificationCompat.Builder mBuilder = new NotificationCompat().Builder(context)
                .setLargeIcon(R.drawable.ALERT)
                .setContentTitle(msg)
                .setTicker(msgAlert)
                .setContentText(msgText);

        mBuilder.setContentIntent(notificIntent);
        mBuilder.setDefaults(NotificationCompat.DEFAULT_VIBRATE);
        mBuilder.setAutoCancel(true);

        NotificationManager mNotificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        mNotificationManager.notify(1,mBuilder.build());

    }
}




2 个答案:

答案 0 :(得分:4)

更改

NotificationCompat.Builder mBuilder = new NotificationCompat().Builder(context)

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)

答案 1 :(得分:0)

new NotificationCompat().Builder()将首先创建NotificationCompat类实例,然后尝试在其中找到名为Builder的内容。

您需要像这样重写此行: new NotificationCompat.Builder()以便new创建在Builder内声明的NotificationCompat类实例。