3

当我尝试将我的通知代码设置为按钮时,它总是给我这部分的错误

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

[[ 构造函数 NotificationCompat.Builder(new View.OnClickListener(){}) 未定义]]

我怎么能解决这个问题???

Button button9= (Button) findViewById(R.id.button9);
    button9.setOnClickListener(new View.OnClickListener(){
    public void onClick(View arg0) {


        ///////////My Notification//////////////////////////        
        NotificationCompat.Builder mBuilder =new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("I'm astm  loooooooool")
        .setContentText("Hello baby to my 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) (ResultActivity)
        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);
        int mId = 0;
        // mId allows you to update the notification later on.
        mNotificationManager.notify(mId, mBuilder.build());
        ///////////End Notification//////////////////////////
        }
    });
4

3 回答 3

2

Thanks guys for helping me [[ No one gives me any answer looooooool ]]

i get the answer with my self just define the NotificationCompat.Builder as Final and it worked well

^__^

        ///////// my Nine button (set Notification) //////////
    final NotificationCompat.Builder mBuilder =new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.notification_icon)
    .setContentTitle("I'm astm  loooooooool")
    .setContentText("Hello baby to my world!");
    Button button9= (Button) findViewById(R.id.button9);
    final TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    button9.setOnClickListener(new View.OnClickListener(){
    public void onClick(View arg0) {

        ///////////My Notification//////////////////////////        
        // Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent();

        // 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.
        // Adds the back stack for the Intent (but not the Intent itself) (ResultActivity)
        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);
        int mId = 0;
        // mId allows you to update the notification later on.
        mNotificationManager.notify(mId, mBuilder.build());
        ///////////End Notification//////////////////////////

      }
    });
于 2013-06-20T08:01:39.373 回答
1

改变this_getBaseContext

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getBaseContext());
于 2016-08-28T08:13:52.320 回答
0

我有同样的问题。我变了:

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

和:

NotificationCompat.Builder mBuilder =new NotificationCompat.Builder(this.context)
于 2015-10-16T05:13:03.367 回答