按下按钮并显示通知

时间:2014-02-18 12:39:06

标签: java android button notifications

我点击xml时有一个按钮代码应该显示一个通知位,它没有运行给出错误(Notification.Builder(new View.OnClickListener(){})未定义) 任何人都告诉我们这个问题

Mainactivity.Java

    package com.example.auto;
    public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

        @Override
        public void onClick(View v) {

Intent intent = new Intent(MainActivity.this, NotificationReceiver.class);
PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this,
        0, intent, 0);

Notification mNotification = new Notification.Builder(this)

                .setContentTitle("New Post!")
                .setContentText("Here's an awesome update for you!")
                .setContentIntent(pIntent)
          .addAction(0, "View", pIntent)
           .addAction(0, "Remind", pIntent)
     .build();
NotificationManager notificationManager = (NotificationManager) 
        getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, mNotification);
}
    });
}
    }

3 个答案:

答案 0 :(得分:0)

试试这个:)

NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(context)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("My notification")
        .setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(getApplicationContext(), MYDEMOACTIVITY.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(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MYDEMOACTIVITY.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.
mNotificationManager.notify(mId, mBuilder.build());

答案 1 :(得分:0)

构造函数需要Context,而OnClickListener不是Context

改变这个:

Notification mNotification = new Notification.Builder(this)

为:

Notification mNotification = new Notification.Builder(MainActivity.this)

答案 2 :(得分:0)

Notification mNotification = new Notification.Builder(this)

你不能使用'this'作为上下文,因为你使用它来引用新的OnClickListener,而不是你的Activity。

要获取封闭的Activity的上下文,您必须使用MainActivity.this:

Notification mNotification = new Notification.Builder(MainActivity.this)