Reputation: 109
This the receiver
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);
}
Here Is Global Class
public class Globals {
public static List <ProductClass> productList=new ArrayList<ProductClass>();
public static String testArray[]={"test1","test2","test3"};
}
App didn't crash but onReceive function Globals.productList.size return 0,I can get data from testArray. the productList arraylist is also static and I can get data from Globals.ProductList in all activity except onReceuve function.
So, How can I reach and get data from Global static ArrayList ?
MainActivity class
@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());
}
Upvotes: 0
Views: 67
Reputation: 777
I believe you need to debug this and watch the changes to your productList
.
From a first look without the rest of the code, I believe on the program start you initialise your list as a new list which creates an empty list of size 0 (Which is being returned to you) so this for me looks quite normal.
The BroadcastReceiver
should be called as follow, you don't create object of it:
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);
Check BroadcastReceiver
documentation for more details.
** Note that saving data to a static list doesn't mean they will stay there after the program is closed, and BroadcastReceiver
could be running while the app is not running that's why you will be able to retrieve the data in the application and not in this class. Maybe you would need to save the data in a database or something to be able to reach it, I believe your problem is here not in the way of retrieving the data. (Again a bit more of code would help me to help you better)
Upvotes: 1