在广播接收器中调用活动字符串方法[重复]
Posted
技术标签:
【中文标题】在广播接收器中调用活动字符串方法[重复]【英文标题】:Call Activity String Method in Broadcast Receiver [duplicate] 【发布时间】:2018-07-15 05:15:32 【问题描述】:如何在我的项目中做到这一点? 我有这样的广播接收器
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();
我在我的活动中设置了 sendpush(),就像这样
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();从我的活动到广播接收器?如果我运行这段代码,它可以完美运行,但是当广播接收器出现时,它会给我一个像这样的错误
Error:(16, 29) 错误:非静态方法sendingpush() 不能 从静态上下文引用
请帮我修复我的代码,谢谢
【问题讨论】:
Non-static variable cannot be referenced from a static context ...或“Non-static method cannot be referenced from a static context” error的可能重复 您正在尝试使用类名调用非静态方法,即sendingpush()
。请仔细阅读上述评论中 Selvin 提供的参考资料
【参考方案1】:
将 sendpush() 提取到另一个名为 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);
【讨论】:
感谢您的回答以上是关于在广播接收器中调用活动字符串方法[重复]的主要内容,如果未能解决你的问题,请参考以下文章