Srinabh
Srinabh

Reputation: 400

getActivity Not found for PendingIntent

I have the header file for pendingIntent

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

This seems to not be working

Here is my whole code

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);



            }
        });
    }
}

Upvotes: 0

Views: 389

Answers (1)

John Joe
John Joe

Reputation: 12803

Remove the new

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

From the answer:

getActivity() is a static method of the class PendingIntent and does not require an instance of PendingIntent in order to be invoked.

Answer may refer here

Upvotes: 1

Related Questions