I have two small questions;
My app's BroadcastReceiver works fine when the app is running, I get the "Tuesday at 2" toast message perfectly and it updates the app, but when I close my app, that means the app stops running, it doesn't get called but instead it crashes "Your app stopped working", so it knows it's supposed to call it, but doesn't, right?
It seems that the BroadcastReceiver gets called everyday at around 2pm, not only on Tuesdays at 2pm, I want the app to update once a week. Is it maybe because I call my setAlarm() method on my UpcomingFragment's onCreate?
In the UpcomingFragment class (where I set the alarm for it to go off each Tuesday at 2)
private void setAlarm(){
AlarmManager alarmMgr = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getActivity(), UpdateReceiver.class);
intent.putExtra("greeting", "Hello");
PendingIntent alarmIntent = PendingIntent.getBroadcast(getActivity(), 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
calendar.set(Calendar.HOUR_OF_DAY, 14);
// With setInexactRepeating(), you have to use one of the AlarmManager interval
// constants--in this case, AlarmManager.INTERVAL_DAY.
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
}
In the BroadcastReceiver's onReceive()
public class UpdateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
UpcomingFragment.getInstance().update();
//if tuesday
Toast.makeText(context, "Tuesday at 2" , Toast.LENGTH_LONG).show();
}
}
Manifest:
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name=".UpdateReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
i can't provide you with the logcat, cause it doesn't print it when it cashes, i don't know if my android studio is at fault here or my app. Thanks!
You can't manipulate Fragment
s inside a BroadCastReceiver
's context. Essentially broadcast receivers are run inside application context and in which you can't access UI stuff.
If you would like to start an Activity once that broadcast is fired, you would need the following code:
Intent intent = new Intent(context, MyActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
Note: Accessing to UI (including Fragments) must be done in an activity context.
Update #1
It seems that the BroadcastReceiver gets called everyday at around 2pm, not only on Tuesdays at 2pm, I want the app to update once a week. Is it maybe because I call my setAlarm() method on my UpcomingFragment's onCreate?
I think I have a better approach than yours.
Getting current time and setting its day of week to Tuesday may result in weird behaviors.
Using a boot completed broadcast receiver to set the alarm may be better.
For the former, your scheduling codes should set the alarm to the nearest coming Tuesday on 2 PM. For this, this answer may be helpful.
For the latter, first, add the following permission to your manifest file.
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Then, define another broadcast receiver and declare it to the manifest file.
<receiver android:name="your.package.name.BootCompletedReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
In your newly created broadcast receiver add this code.
@Override
public void onReceive(Context context, Intent intent) {
// Set the alarm to coming Tuesday on 2 PM
}
This ensures your app to get updated on every Tuesday on 2 PM in all circumstances.