Java – call mainactivity method (extend broadcast receiver) from other classes

I'm trying to call mainactivity's method display from another class (phonestatereceiver)_ notification.

But got this error:

Error pointing to this row in mainactivity

Intent resultIntent = new Intent(getApplicationContext(),MainActivity. class);

But I tried to change getapplicationcontext () to "this" and "getactivity ()", but it didn't work properly

This is the complete code

MainActivity. java

public class MainActivity extends ActionBarActivity {

    public void display_notification(String incoming_number) {

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(getApplicationContext())
                    .setSmallIcon(R.drawable.ic_action)
                    .setContentTitle("SpamBlocker alert !!")
                    .setContentText("SpamBlocker blocked number : " + incoming_number);

    // **Error occured in following line**
    Intent resultIntent = new Intent(getApplicationContext(),MainActivity.class);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                    0,PendingIntent.FLAG_UPDATE_CURRENT
            );
    mBuilder.setContentIntent(resultPendingIntent);
    notificationmanager mnotificationmanager =
            (notificationmanager) getSystemService(Context.NOTIFICATION_SERVICE);
    Random r = new Random();
    int rand = r.nextInt(1000);
    mnotificationmanager.notify(rand,mBuilder.build());
    }
}

PhoneStateReceiver. java

public class PhoneStateReceiver extends BroadcastReceiver {

    MainActivity mActivity;

    @Override
    public void onReceive(Context context,Intent intent) {
        mActivity = new MainActivity();
        mActivity.display_notification(incomingNumber);           // call main activity methods

    }
}

Solution

You do not use the context provided by onReceive Try to pass the context to display_ Notification instead of using the active context, which may not run when you receive the broadcast

So change the method signature to:

public void display_notification(String incoming_number,Context context)

In addition, the context is passed from onReceive as follows:

mActivity.display_notification(incomingNumber,context);

Use this context when you pass it to intent:

Intent resultIntent = new Intent(context,MainActivity.class);

EDITED

Put the entire method in the receiver and use the context provided by onReceive

or

Change the row to:

notificationmanager mnotificationmanager =
        (notificationmanager) context.getSystemService(Context.NOTIFICATION_SERVICE);

The important part is context Getsystemservice, where the context comes from onReceive

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>