0
votes

I want to call the notify() method which is in MainActivity from another class. This is how I did it:

public class MyAlarmService extends Service{
    @Override
    public IBinder onBind(Intent arg0)
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate()
    {
        // TODO Auto-generated method stub
        super.onCreate();
    }

    @SuppressWarnings("static-access")
    @Override
    public void onStart(Intent intent, int startId)
    {
        super.onStart(intent, startId);

        String title = "title";
        String message = "message";

      new  MainActivity().Notify(title, message);
...

MainActivity:

public class MainActivity extends Activity {
  NotificationManager manager;
    Notification myNotication;

  @Override

    protected void onCreate(Bundle savedInstanceState) {
        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

...
 public void Notify(String notificationTitle, String notificationMessage){
        Intent intent = new Intent("com.xxxx.app.MainActivity");

        PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 1, intent, 0);

        Notification.Builder builder = new Notification.Builder(MainActivity.this);

        builder.setAutoCancel(false);
        builder.setTicker("this is ticker text");
        builder.setContentTitle("Notification");
        builder.setContentText("Text");
        builder.setSmallIcon(R.drawable.infoicon);
        builder.setContentIntent(pendingIntent);
        builder.setOngoing(true);
        builder.setSubText("This is subtext...");
        builder.setNumber(100);
        builder.build();
        myNotication = builder.getNotification();
        manager.notify(11, myNotication);
    }

...

But I get an error message:

FATAL EXCEPTION: main Process: com.xxxx.app, PID: 10311 java.lang.RuntimeException: Unable to start service com.xxxx.app.MyAlarmService@636e513 with Intent { cmp=com.xxxx.app/.MyAlarmService }: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3027) at android.app.ActivityThread.-wrap17(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1442) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference at android.content.ContextWrapper.getPackageName(ContextWrapper.java:133) at android.app.PendingIntent.getActivity(PendingIntent.java:305) at android.app.PendingIntent.getActivity(PendingIntent.java:272) at com.xxxx.app.MainActivity.Notify(MainActivity.java:304) at com.xxxx.app.MyAlarmService.onStart(MyAlarmService.java:41) at android.app.Service.onStartCommand(Service.java:459) at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3010) at android.app.ActivityThread.-wrap17(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1442)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5417)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

2
1) Copy that Notify method in the service.and call it wherever you need. In order to trigger notification, Notification Service needs Context either Activity or service) - Chaitu
Intent intent = new Intent("com.xxxx.app.MainActivity"); That is one way to make an Intent, but it is error prone. - OneCricketeer
Along with that, new MainActivity().Notify(title, message); is causing the error because you should never do new MainActivity() because it isn't managed by the Android life cycle - OneCricketeer

2 Answers

1
votes

You can do the same in following ways:

1) Copy that Notify method into the service and call it wherever you need order to trigger notification, Notification Service needs Context either Activity or service)

2) You can communicate to the activity from the service through broadcast receivers

3) You can do by using aidl

1
votes

Trigger the notification in the Service class inside the onStart() method, and start the service from the MainActivity class using an Intent. Also, don't forget to declare your service in the manifest.

Here's the code to start the service from activity: Intent intent = new Intent(this, MyAlarmService.class); startService(intent);