android - 扩展 BroadcastReceiver onReceive 函数无法从其他类获取静态 ArrayList
问题描述
这个接收器
public class Alarm extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire();
NotificationClass.createNotification("Total Item In List is :"+Globals.productList.size(),context);
wl.release();
}
public void setAlarm(Context context) {
Log.i("ALARM", "Alarm has been set it");
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, Alarm.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 1 * 1, pi);
}
public void cancelAlarm(Context context)
{
Log.i("ALARM","Alarm has been Cancel");
Intent intent = new Intent(context, Alarm.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}
这里是全局类
public class Globals {
public static List <ProductClass> productList=new ArrayList<ProductClass>();
public static String testArray[]={"test1","test2","test3"};
}
应用程序没有崩溃,但 onReceive 函数 Globals.productList.size 返回 0,我可以从 testArray 获取数据。productList arraylist 也是静态的,我可以在除 onReceuve 函数之外的所有活动中从 Globals.ProductList 获取数据。
那么,我怎样才能从 Global static ArrayList 中获取和获取数据?
MainActivity 类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GetDataClass.getAllData();
List <ProductList> pList=new ArrayList<ProductList>();
pList=GetDataClass.data;
for(int i=0;i<GetDataClass.data.size();i++)
{
ProductList temp=new ProductList();
temp.set(pList.get(i).pName,pList.get(i).stock,
pList.get(i).provider,pList.get(i).jk,pList.get(i).starCounter);
Globals.productList.add(temp);
}
Log.i("Count", String.valueOf(Globals.productList.size()));//Return Correct number.
Alarm alarm=new Alarm();
alarm.setAlarm(getApplicationContext());
}
解决方案
我相信你需要调试它并观察你的productList
.
从没有其余代码的第一眼看,我相信在程序开始时,您将列表初始化为一个新列表,该列表创建一个大小为 0 的空列表(将返回给您),所以这对我来说看起来很正常。
BroadcastReceiver
应该如下调用,你不创建它的对象:
Intent intent = new Intent(getApplicationContext(), Alarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(appContext, alarmRequestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendarTime, pendingIntent);
查看BroadcastReceiver
文档以获取更多详细信息。
** 请注意,将数据保存到静态列表并不意味着它们会在程序关闭后保留在那里,并且BroadcastReceiver
可能会在应用程序未运行时运行,这就是为什么您将能够在应用程序中检索数据而不是在这节课。也许您需要将数据保存在数据库中或能够访问它的东西中,我相信您的问题不在于检索数据。(再多一点代码会帮助我更好地帮助你)
推荐阅读
- php - 将 ACF 添加到自定义函数
- c++ - 如何在 Dev C++ 中显示甘特图?
- java - Netbeans Java 模块化项目 - 资源文件夹
- kubernetes - 在 AKS 中缩减后将所有 pod 移动到同一节点
- java - Java POI Excel。获取具有格式化日期的单元格的实际文本值
- python - Pygame 错误:'int' 对象不可调用
- mysql - 在 mysql 数据库中,您可以将列中某个值的出现次数计算为自动生成的列吗?
- r - R动态地将具有NA的多列合并为一列
- java - 如何反序列化扩展 Spring Boot 中的通配符泛型类型
- botframework - 无论如何要增加 input.text 元素的大小?