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

时间:2018-02-05 10:58:16

标签: android android-activity broadcastreceiver

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

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()不能   从静态上下文引用

请帮我修改我的代码,谢谢

1 个答案:

答案 0 :(得分:1)

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