0

I am trying to Call method from Another Activity but it is not calling. When i use intent it works but when i call method it fails. The method which is in onNotificationPosted(). Here is the code

public class NotificationListener extends NotificationListenerService {

    Context context;
    private DatabaseReference dbref;
    private FirebaseAuth mAuth;

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        dbref= FirebaseDatabase.getInstance().getReference();
        mAuth=FirebaseAuth.getInstance();
    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        ChatActivity ch=new ChatActivity();
        ch.location();
      //  Intent n=new Intent(NotificationListener.this,ChatActivity.class);
        //startActivity(n);
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        Log.i("Msg","Notification Removed");
    }
}

The intent which is commented works but the method is not calling. Please help me. Thanks in advance.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
Msb Says
  • 11
  • 1
  • 7
  • Doesn't look like you can instantiate an activity and call it's method like that. checkout this post for an explanation of your issue: https://stackoverflow.com/questions/19666572/how-to-call-a-method-in-another-activity-from-activity – bryant Mar 30 '18 at 17:47
  • Thanks but i already viewed this post but no luck :( – Msb Says Mar 30 '18 at 17:51
  • I cannot make this actiivity as class because it is notificationListener – Msb Says Mar 30 '18 at 17:52
  • Please describe your use case a little. Why do you need to call the method from the `ChatActivity`. As far as I could understand, launching the `ChatActivity` suffices your purpose. – Reaz Murshed Mar 30 '18 at 18:02
  • I dont want to start activity .. i only need to run the method in chat activity because that will run in background – Msb Says Mar 30 '18 at 18:09
  • You cannot simply call an @Override method. – Rahulrr2602 Mar 30 '18 at 22:03

1 Answers1

0

make the method static and then call it like

ChatActivity.ch();