I'm developing an android app, and part of it involves a notification that reminds the user to do something. This notification at a specified time and repeats every 12 hours. I'm using AlarmManager to schedule the alarm and I've also included the code to start my Alarm Service when the device boots. Here are my java classes:
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
int i = preferences.getInt("numberoflaunches", 1);
if (i < 2) {
alarmMethod();
i++;
editor.putInt("numberoflaunches", i);
editor.commit();
}
}
private void alarmMethod() {
Intent intent = new Intent(this, AlarmService.class);
this.startService(intent);
Toast.makeText(MainActivity.this, "Alarm Set", Toast.LENGTH_SHORT).show();
}
}
AlarmService.java
public class AlarmService extends Service {
//used for register alarm manager
PendingIntent pendingIntent;
//used to store running alarm manager instance
AlarmManager alarmMgr;
//Callback function for alarm manager event
BroadcastReceiver mReceiver;
private static final String TAG = "MyService";
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
//Register AlarmManager Broadcast receive.
RegisterAlarmBroadcast();
}
#Override
public void onStart(Intent intent, int startid) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.HOUR_OF_DAY, 6);
alarmMgr.cancel(pendingIntent);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 1000 * 60 * 60 * 12, pendingIntent);
}
private void showNotification() {
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("app_name")
.setContentText("something")
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT))
.setSound(soundUri)
.setSmallIcon(R.drawable.notification_icon)
.setAutoCancel(true)
.setOnlyAlertOnce(true)
.build();
NotificationManagerCompat.from(this).notify(0, notification);
}
private void RegisterAlarmBroadcast() {
Log.i("RegisterAlarmBroadcast", "Register Intent.RegisterAlarmBroadcast");
//This is the call back function(BroadcastReceiver) which will be called when your alarm time is reached.
mReceiver = new BroadcastReceiver() {
private static final String TAG = "Alarm Example Receiver";
#Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "BroadcastReceiver::OnReceive() >>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
showNotification();
}
};
//Register the alarm broadcast here
registerReceiver(mReceiver, new IntentFilter("com.example.application.myNotification"));
pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent("com.example.application.myNotification"), 0);
alarmMgr = (AlarmManager) (this.getSystemService(Context.ALARM_SERVICE));
}
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
}
}
autostart.java
public class autostart extends BroadcastReceiver {
#Override
public void onReceive(Context arg0, Intent arg1)
{
Intent intent = new Intent(arg0,AlarmService.class);
arg0.startService(intent);
Log.i("Autostart", "started");
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.application" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".autostart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name=".AlarmService"
android:enabled="true" />
</application>
However I must have done something wrong as it is not working properly. My problems are:
1. When I exit the app, the notification for some reason goes off, whatever the time is.
2. When I reboot, the notification goes off, whatever the time is.
I don't know if these problems are in any way related, maybe I have a piece of code that's messing everything up. But either way I would really appreciate any sort of help I can get. Thanks in advance.
The repeating interval should be 24 hours (1000 * 60 * 60 * 24), not 12 hours.
You are individually setting the time to 6 AM. So you can remove the line calendar.setTimeInMillis(System.currentTimeMillis());
EDIT:
I have made some of modifications to your code and finally got it working.
You problem was that you set an alarm for 6:00 AM. But you are setting this alarm at a time after 6:00 AM (say 9:00 AM). That is, you are setting an alarm for a past time. So it will go off immediately.
I made a work around for this. If the time you need to set alarm is past, set the alarm to trigger on the next day.
This is my modified code.
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
int i = preferences.getInt("numberoflaunches", 1);
if (i < 2) {
alarmMethod();
i++;
editor.putInt("numberoflaunches", i);
editor.commit();
}
}
private void alarmMethod() {
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
new Intent("com.example.application.myNotification"),
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmMgr = (AlarmManager) (this
.getSystemService(Context.ALARM_SERVICE));
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.HOUR_OF_DAY, 6);
long mills = calendar.getTimeInMillis();
if (mills <= System.currentTimeMillis()) {
Calendar c1 = calendar;
c1.add(Calendar.DAY_OF_MONTH, 1);
mills = c1.getTimeInMillis();
} else {
mills = calendar.getTimeInMillis();
}
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, mills,
1000 * 60 * 60 * 24, pendingIntent);
Toast.makeText(MainActivity.this, "Alarm Set", Toast.LENGTH_SHORT)
.show();
}
}
Autostart.java
public class Autostart extends BroadcastReceiver {
#Override
public void onReceive(Context arg0, Intent arg1)
{
PendingIntent pendingIntent = PendingIntent.getBroadcast(arg0, 0, new Intent("com.example.application.myNotification"), 0);
AlarmManager alarmMgr = (AlarmManager) (arg0.getSystemService(Context.ALARM_SERVICE));
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.HOUR_OF_DAY, 6);
long mills = calendar.getTimeInMillis();
if (mills <= System.currentTimeMillis()) {
Calendar c1 = calendar;
c1.add(Calendar.DAY_OF_MONTH, 1);
mills = c1.getTimeInMillis();
} else {
mills = calendar.getTimeInMillis();
}
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, mills, 1000 * 60 * 60 * 24, pendingIntent);
Log.i("Autostart", "started");
}
}
Alarmer.java
public class Alarmer extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
showNotification(context);
}
private void showNotification(Context context) {
Random r = new Random();
int r0 = r.nextInt();
Uri soundUri = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle("app_name")
.setContentText("something" + r0)
.setContentIntent(
PendingIntent.getActivity(context, 0, new Intent(context,
MainActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT))
.setSound(soundUri).setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true).setOnlyAlertOnce(true).build();
// NotificationManagerCompat.from(this).notify(0, notification);
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(r0, notification);
}
}
AndroiManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.alarmtest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".autostart" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver android:name=".Alarmer" android:enabled="true">
<intent-filter>
<action android:name="com.example.application.myNotification" />
</intent-filter>
</receiver>
<service
android:name=".AlarmService"
android:enabled="true" />
</application>
</manifest>
Compare it with your code and make changes.
Related
I need to create an application to run a method at a specific time. However, AlarmManager never works, and I do not understand why. I set up the alarm to run at 11.54, but it always runs when the application starts and never runs at the expected time. If you know what is the problem, please comment. Thank you very much.
This is my Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.spr.timemanager">
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:allowBackup="true"
android:dataExtractionRules="#xml/data_extraction_rules"
android:fullBackupContent="#xml/backup_rules"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.TimeManager"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".AlarmReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.ALARM"/>
</intent-filter>
</receiver>
</application>
</manifest>
And this is my MainActivity.java
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 11);
calendar.set(Calendar.MINUTE, 54);
Intent intent = new Intent(this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0 , intent, 0);
time = (calendar.getTimeInMillis() - (calendar.getTimeInMillis() % 60000));
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Log.e("time", String.valueOf(time));
time = System.currentTimeMillis();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,time,pendingIntent);}
else {
alarmManager.setExact(AlarmManager.RTC_WAKEUP,time,pendingIntent);}
}else {
alarmManager.set(AlarmManager.RTC_WAKEUP,time,pendingIntent);
}
My AlarmReceiver
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Vibrator vibrator = (Vibrator) context.getSystemService(context.VIBRATOR_SERVICE);
vibrator.vibrate(4000);
Log.e("System time", String.valueOf(System.currentTimeMillis()));
}
}
This is my logcat
2022-05-25 11:52:27.820 16914-16914/com.spr.timemanager E/time: 1653468840000
2022-05-25 11:52:28.103 16914-16914/com.spr.timemanager E/System time: 1653468748103
P/S: if I change these alarmManager.set() code into setreapeating(), it always fires when the application starts and 5 seconds afterward, no matter how I set it.
you are calculating your time (when alarm should fire)
time = (calendar.getTimeInMillis() - (calendar.getTimeInMillis() % 60000));
but at very last after logging you are overriding this variable with
time = System.currentTimeMillis();
just before if cascade. remove this line, looks unnecessary
btw. HOUR_OF_DAY is in <0 to 23>, so if you want 11:54 exacly then you should set 10. you can also set seconds and millis with Calendar.SECOND and Calendar.MILLISECOND to 0 instead of dividing with % and then your time = calendar.getTimeInMillis()
ps. you should add some alarm-in-past-setting protection (add a day?)
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 10); // remember set -1 in here!!
calendar.set(Calendar.MINUTE, 54);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
// ensuring next day
while (calendar.getTimeInMillis() < System.currentTimeMillis()) {
calendar.add(Calendar.DATE, 1);
}
time = calendar.getTimeInMillis();
Log.e("time", String.valueOf(time));
Intent intent = new Intent(this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0 , intent, 0);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
... set alarm for time
It's supposed to be a simple to do list app. You can set a list Item with a task and time. It should send you a notification at the given time.
Everything works fine when the app is running but when I close it I don't get notifications. I tried on API 23 and API R. I was looking at other posts but I couldn't find any solution.
Receiver class:
public class AlertReceiver extends BroadcastReceiver {
NotificationManagerCompat notificationManager;
#Override
public void onReceive(Context context, Intent intent) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
boolean is24HourFormat = android.text.format.DateFormat.is24HourFormat(context);
CharSequence setTime;
if (is24HourFormat) {
setTime = DateFormat.format("HH:mm", calendar);
} else {
setTime = DateFormat.format("hh:mm a", calendar);
}
String currentTime = setTime.toString();
DataBaseHelper dataBaseHelper = new DataBaseHelper(context);
Item itemForNotificationText = dataBaseHelper.GetNotificationContent(currentTime);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
Notification notification = new NotificationCompat.Builder(context,CHANNEL_1_ID)
.setSmallIcon(R.drawable.ic_baseline_notifications_active_24)
.setContentTitle(itemForNotificationText.getTask())
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setContentText(itemForNotificationText.getTime())
.build();
notificationManager.notify(dataBaseHelper.GetID(),notification);
}
}
I tired using setExactAndAllowWhileIdle but still doesn't work.
Start alarm:
public void startAlarm (Calendar c){
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlertReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, item.getNotification_id(), intent,0);
if (c.before(Calendar.getInstance())) {
c.add(Calendar.DATE, 1);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pendingIntent);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pendingIntent);
}
}
Manifest file:
<application
android:name=".App"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".InsertActivity"></activity>
<activity
android:name=".ToDoList"
android:parentActivityName=".MainActivity" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".AlertReceiver"/>
</application>
APP Class(Notification channel is created here):
public class App extends Application {
public static final String CHANNEL_1_ID = "Channel 1";
#Override
public void onCreate() {
super.onCreate();
CreateNotificationChannels();
}
private void CreateNotificationChannels () {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel1 = new NotificationChannel(
CHANNEL_1_ID,
"Channel 1",
NotificationManager.IMPORTANCE_HIGH
);
channel1.setDescription("Notifies you when an event has started.");
channel1.enableVibration(true);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel1);
}
}
}
i set two different alarms that should make 2 notifications
8:34:0 pm create notification 1 with title and msg..
8:34:10 pm create notification 2 with title and msg...
the problem is they both appear together at 8:34:00 pm
(at the first alarm time (both))!!!
what i did:-
1- i made a broadcast class with notification manager to build notification
2- main activity created alarmManager and calendar instance to set time for alarm.
public class MainActivity extends AppCompatActivity {
#RequiresApi(api = Build.VERSION_CODES.KITKAT)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Creating alarm method(requestcode,hours,min,seconds)
setAlarmo(1,20,34,0);
setAlarmo(2,20,34,10);
}
// the Alarm Method
public void setAlarmo(int reqcode, int hour,int minute,int second){
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,hour);
calendar.set(Calendar.MINUTE,minute);
calendar.set(Calendar.SECOND,second);
long timeinMillis = calendar.getTimeInMillis();
Intent intent = new Intent(getApplicationContext(),AlertRec.class);
intent.putExtra("reqcode",reqcode);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),reqcode,intent,PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(alarmManager.RTC_WAKEUP,timeinMillis,pendingIntent);
}
}
#
// AlertClass receiver extending broadcastReceiver
public class AlertRec extends BroadcastReceiver {
#Override
public void onReceive( final Context context, Intent intent) {
// Creating receiveAlarm method(requestCode,Context,intent,title,text for notification)
//receiving first Alarm from MainActivity.Class
recAlarmo(1, context,intent,"title 1","im msg 1");
//receiving Second Alarm from MainActivity.Class
recAlarmo(2,context,intent,"title 2","im msg 2");
}
// receiving Alarms Method and Creating Notifications
public void recAlarmo(int reqcode , Context context,Intent intent,String title,String msg){
intent = new Intent(context,MainActivity.class);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
PendingIntent pendingIntent = PendingIntent.getActivity(context.getApplicationContext(),reqcode,intent,PendingIntent.FLAG_ONE_SHOT);
builder.setContentTitle(title);
builder.setSmallIcon(android.R.drawable.star_on);
builder.setContentText(msg);
builder.setAutoCancel(true);
builder.setContentIntent(pendingIntent);
builder.setDefaults(NotificationCompat.DEFAULT_SOUND);
NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(reqcode,builder.build());
}
}
#
<uses-permission android:name="android.permission.SET_ALARM"></uses-permission>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".AlertRec"></receiver>
</application>
i solved it !
------------------------------
in MainActivity
1-create a method that takes int number
and inside the method make a pending intent and within the intent of the pending intent
pass the int number as intent.putExtra("id",number);
public void Method(int id ,int hour , int minute, int seconds){
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,hour);
calendar.set(Calendar.MINUTE,minute);
calendar.set(Calendar.SECOND,second);
Intent alertIntent = new Intent(getApplicationContext(), AlertRec.class);
alertIntent.putExtra("id",id);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),PendingIntent.getBroadcast(getApplicationContext(),id,alertIntent,PendingIntent.FLAG_UPDATE_CURRENT));
}
----------------------------------------------------
2- at the Receiver class
//override the OnReceive method
onReceive(Context context,Intent intent ){
int Checker =intent.getExtras.getInt("id");
if(Checker == 1 ) {
// do anything you want for the intent of the pending intent at MainActivity that have int id = 1;
//your method here
}else if(Checker == 2) {
// do anything you want for the intent of the pending intent at MainActivity that have int id = 2;
// your method here
}else{
}
}
Went through the documentation on how to start a service automatically after the device boots and tried all they said but couldn't get my service to work after rebooting the device, though it works perfectly well on first instance.
Below is my main activity (MainActivity.java) which starts the service after a button has been clicked, and triggers the service every 10 seconds using AlarmManager.
public void onClickSubmitDate(View view) {
Intent service = new Intent(this, MyService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, service, 0);
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 0, 10 * 1000, pendingIntent);
//Enable receiver when device boots
ComponentName receiver = new ComponentName(this, BootReceiver.class);
PackageManager pm = this.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
Below is my receiver class (BootReceiver.java)
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
Intent service = new Intent(context, MyService.class);
PendingIntent pendingIntent = PendingIntent.getService(null, 0, service, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
10 * 1000, 10 * 1000,
pendingIntent);
}
}
}
My service contains a thread which checks for multiple scenarios and builds a notification for each scenario. Here's a sample below:
public int onStartCommand(Intent intent, int flags, int startId) {
notification = new NotificationCompat.Builder(this);
notification.setAutoCancel(true);
Runnable r = new Runnable() {
#Override
public void run() {
try {
boolean diff = true;
if (diff) {
// Build the notification
notification.setSmallIcon(R.drawable.icon);
notification.setTicker(getString(R.string.notification_ticker));
notification.setWhen(System.currentTimeMillis());
notification.setContentTitle(getString(R.string.notification_title));
notification.setContentText(getString(R.string.notification_1_text));
notification.setSound(alarmSound);
Intent i = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(pendingIntent);
// Builds a notification and issues it
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Random r = new Random();
int rand = r.nextInt(1000);
nm.notify(rand, notification.build());
}
catch (Exception e) {
e.printStackTrace();
}
}
};
Thread stephThread = new Thread(r);
stephThread.start();
return START_STICKY;
}
And finally below is my AndroidManifest.xml code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.motherslove.stephnoutsa.myapplication18calendartest"
android:installLocation="internalOnly">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MyDate"
android:label="#string/title_activity_my_date"
android:theme="#style/AppTheme.NoActionBar" />
<service
android:name=".MyService"
android:enabled="true"
android:exported="true" />
<receiver
android:name=".BootReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
</manifest>
Please can someone tell me what I've done wrong?
Thanks to help from #MikeM I could fix the problem.
Instead of
PendingIntent pendingIntent = PendingIntent.getService(null, 0, service, 0);
I had to pass in the context as the first argument, in the receiver, as shown below
PendingIntent pendingIntent = PendingIntent.getService(context, 0, service, 0);
I have a general question about setting up a daily data check from my app (giving a notification when some event occurs).
My current approach is:
From the app (onCreate) I start a service
From the service I set an alarm using AlarmManager after checking if there already is a matching PendingIntent (in which case I don't
need to set a new alarm)
The service checks the data from SharedPreferences and eventually shows a notification
For testing purposes I set the alarm to +1 minute ... but it didn't work that way. Maybe I'm doing something wrong conceptionally?
Some code
AndroidManifest.xml
<application
<activity
android:name=".OverviewActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar"
android:configChanges="orientation"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="xxxxx.CheckDaysMonths" >
</service>
</application>
Service CheckDaysMonths
public class CheckDaysMonths extends Service {
private static int ALARM_ID = 131313;
private static String DECLARED_ACTION = "xxxxx.CheckDaysMonths";
#Override
public void onCreate() {
super.onCreate();
PendingIntent alarmIntent = AlarmHelper.getPendingIntentFromAlarm(this, ALARM_ID, DECLARED_ACTION);
if(alarmIntent == null) {
if(someConditionIsMet)
sendNotification(" my notification text ");
// Set an alarm for the next time this service should run:
setAlarm();
}
stopSelf();
}
public void setAlarm() {
AlarmHelper.setAlarm(this, ALARM_ID, DECLARED_ACTION, 8);
}
public void sendNotification(String notifyText) {
Intent mainIntent = new Intent(this, OverviewActivity.class);
#SuppressWarnings("deprecation")
Notification noti = new Notification.Builder(this)
.setAutoCancel(true)
.setContentIntent(PendingIntent.getActivity(this, 131314, mainIntent,
PendingIntent.FLAG_UPDATE_CURRENT))
.setContentTitle("Gratuliere!")
.setContentText("Du bist seit " + notifyText + " rauchfrei!")
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(R.mipmap.ic_launcher)
.setTicker("Du bist seit " + notifyText + " rauchfrei!")
.setWhen(System.currentTimeMillis())
.getNotification();
NotificationManager notificationManager
= (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(131315, noti);
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
Inside the main activity (OverviewActivity.java)
EDIT: Changed service call from implicit to explicit
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_overview);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
startService(new Intent(this, CheckDaysMonths.class));
// additional stuff happening on the activity of my app goes here:
...
}
AlarmHelper.java
public class AlarmHelper {
public static PendingIntent getPendingIntentFromAlarm(Context context, int alarmId, String declaredAction) {
return (PendingIntent.getService(context, alarmId,
new Intent(declaredAction),
PendingIntent.FLAG_NO_CREATE));
}
public static void setAlarm(Context context, int alarmId, String declaredAction, int hourOfDay) {
Intent serviceIntent = new Intent(declaredAction);
PendingIntent pi = PendingIntent.getService(context, alarmId, serviceIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
// How long until tomorrow to its hourOfDay?
//DateTime tomorrow = (new DateTime()).plusDays(1);
//DateTime tomorrowAtHourOfDay = new DateTime(tomorrow.getYear(), tomorrow.getMonthOfYear(), tomorrow.getDayOfMonth(), hourOfDay, 0);
DateTime tomorrowAtHourOfDay = (new DateTime()).plusHours(3);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, tomorrowAtHourOfDay.getMillis(), pi);
}
}
I fixed it by introducing another "middle tier" service
So now I have
The app GUI starting service 1
Service 1 will check if there is already an alarm with the correct signature running. If not, it will start an alarm
The alarm will fire at 2am and start service 2
Service 2 will check the data (SharedPreferences) and eventually send a notification. Then it will start the alarm again
A BroadcastReceiver for BOOT starts Service 2
This works quite well