-1

How to do this in my project? I have Broadcast Receiver like this one

public class AlarmManagerBroadcastReceiver extends BroadcastReceiver {

    public AlarmManagerBroadcastReceiver(){
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Broadcast Receiveds", Toast.LENGTH_LONG).show();
        ActivityReminderBaru.sendingpush();
    }
}

and I set the sendingpush() in my activity like this one

public class ActivityReminderBaru extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reminder_baru);

    }

    public void sendingpush() {
        Intent intentnotif = new Intent(this, UserActivity.class);
        intentnotif.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intentnotif,
                PendingIntent.FLAG_ONE_SHOT);
        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("Staf Operasi 13")
                .setContentText(KeteranganHolder + TanggalHolder + WaktuHolder)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }

}

the question is, how to call sendingpush(); from my activity to broadcast receiver? if I run this code it work perfect, but when the broadcast receiver come up it gives me an error like this one

Error:(16, 29) error: non-static method sendingpush() cannot be referenced from a static context

please help me to fix my code, thanks

Forumesia
  • 73
  • 1
  • 11
  • 2
    Possible duplicate of [Non-static variable cannot be referenced from a static context](https://stackoverflow.com/questions/2559527/non-static-variable-cannot-be-referenced-from-a-static-context) ... or [“Non-static method cannot be referenced from a static context” error](https://stackoverflow.com/questions/4922145/non-static-method-cannot-be-referenced-from-a-static-context-error) – Selvin Feb 05 '18 at 10:58
  • Your are trying to call the non-static method i.e `sendingpush()` with the Class name. Please go through the reference provided by Selvin in the above comment – Chithra B Feb 05 '18 at 11:02

1 Answers1

1

Extract sendingpush() to a different class called SendPushUtil

public class SendPushUtil {

    public static void sendingpush(Context context) {
        Intent intentnotif = new Intent(context, UserActivity.class);
        intentnotif.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0 /* Request code */, intentnotif,
                PendingIntent.FLAG_ONE_SHOT);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("Staf Operasi 13")
                .setContentText(KeteranganHolder + TanggalHolder + WaktuHolder)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);
        NotificationManager notificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
}

and then use

public class AlarmManagerBroadcastReceiver extends BroadcastReceiver {

    public AlarmManagerBroadcastReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        //Toast.makeText(context, "Broadcast Receiveds", Toast.LENGTH_LONG).show();
        SendPushUtil.sendingpush(context);
    }
}
FilippoE
  • 41
  • 5