在广播接收器中调用活动字符串方法



如何在我的项目中执行此操作?我有这样的广播接收器

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

,我在这个活动中设置了sendingpush((

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

问题是,如何致电sendingpush((;从我的活动到广播接收器?如果我运行此代码,它可以完美地工作,但是当广播接收器出现时,它会给我一个错误的错误

错误:(16,29(错误:非静态方法sendingpush((不能为 从静态上下文引用

请帮助我修复我的代码,谢谢

将sendingpush((提取到名为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());
    }
}

然后使用

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

相关内容

最新更新