未找到PendingIntent

时间:2017-12-04 15:51:42

标签: java android

我有pendingIntent

的头文件
 Intent i=new Intent();
               PendingIntent pi= new PendingIntent.getActivity(MainActivity.this,0,i,0);

这似乎无法正常工作

这是我的整个代码

package com.example.srinabh.notof;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    Button b;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b=findViewById(R.id.notif);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i=new Intent();
               PendingIntent pi= new PendingIntent.getActivity(MainActivity.this,0,i,0);

                Notification n=new Notification.Builder(MainActivity.this)
                        .setTicker("Ticker Title")
                        .setContentTitle("Content Titlle")
                        .setContentText("Content Text")
                        .setContent(pi)
                        .getNotification();
                n.flags=Notification.FLAG_AUTO_CANCEL;
                NotificationManager nm= (NotificationManager)getSystemService(NOTIFICATION_SERVICE);



            }
        });
    }
}

1 个答案:

答案 0 :(得分:1)

删除new

  PendingIntent pi= PendingIntent.getActivity(MainActivity.this,0,i,0);

答案:

  

getActivity()是PendingIntent类的静态方法   不需要PendingIntent实例才能被调用。

答案可以参考here