how to repeat alarm in background - android

I am trying an alarm for every fifteen minute and problem is that I hear that alarm will work when app is not running, but in my case alarm works first time and even second time if app is on screen. else not.also I want to know when first alarm is set and time was in previous time,alarm will start in recent time, but if I set pending intent for 24 hours and at that time mobile was shut down and I turned my mobile after one or two hours, will pending intent that was for a day work after 25 or 26 hours or not.please help
here is my code.
public class MainActivity extends AppCompatActivity {
Calendar calendar=Calendar.getInstance();
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.bt);
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY,13);
calendar.set(Calendar.MINUTE,54);
calendar.set(Calendar.SECOND,1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY,13);
calendar.set(Calendar.MINUTE,54);
calendar.set(Calendar.SECOND,1);
setAlarm(calendar.getTimeInMillis());
}
});
}
private void setAlarm(long time) {
calendar.setTimeInMillis(System.currentTimeMillis());
//getting the alarm manager
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
//creating a new intent specifying the broadcast receiver
Intent i = new Intent(this, MyAlarm.class);
//creating a pending intent using the intent
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
//setting the repeating alarm that will be fired every day
am.setRepeating(AlarmManager.RTC, time, AlarmManager.INTERVAL_FIFTEEN_MINUTES, pi);
Toast.makeText(this, "Alarm is set", Toast.LENGTH_SHORT).show();
}
}
and here is my service
public class MyAlarm extends BroadcastReceiver{
MediaPlayer mp;
#Override
public void onReceive(Context context,Intent intent){
mp=MediaPlayer.create(context,R.raw.song);
mp.start();
}
}
manifest is here
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bilal.pkr">
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></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.VIEW"/>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.BOOT_COPLETED"></action>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".MyAlarm"
android:enabled="true"
android:exported="true" />
</application>
</manifest>

am.setRepeating(AlarmManager.RTC_WAKEUP, time, AlarmManager.INTERVAL_FIFTEEN_MINUTES, pi);
Use this.

Related

how to schedule a task every night at 12 AM

I want to start ScheduledExecutorService exactly on 12 AM daily, but in my code schedule is not started. Can any one tell me Whether my code is Correct or not? If any other way available that would also helpful
public class MainActivity extends AppCompatActivity{
ScheduledExecutorService scheduler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scheduler = Executors.newScheduledThreadPool(1);
scheduleTask();
}
public void scheduleTask() {
final Runnable beeper = new Runnable() {
public void run() {
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(),"SCHEDULE STARTED",Toast.LENGTH_LONG).show();
}
}
}
}, 1500);
}
});
}
};
Calendar midnight = Calendar.getInstance();
midnight.set(Calendar.HOUR_OF_DAY, 0);
midnight.set(Calendar.MINUTE, 0);
midnight.set(Calendar.SECOND, 0);
midnight.set(Calendar.MILLISECOND, 1);
midnight.set(Calendar.DAY_OF_YEAR, midnight.get(Calendar.DAY_OF_YEAR) + 1);
long tillMidnight = midnight.getTimeInMillis() - System.currentTimeMillis() - 1;
long ticksTillMidnight = tillMidnight / 50;
final ScheduledFuture<?> beeperHandle = scheduler.scheduleAtFixedRate(beeper, ticksTillMidnight,20*60*60*24, TimeUnit.MILLISECONDS
);
scheduler.schedule(new Runnable() {
public void run() {
beeperHandle.cancel(true);
}
}, 6*6, TimeUnit.MILLISECONDS
);
}}
The answer is quite simple. In your case, a ScheduledExecutorService is bound to the lifetime of your process. If your process gets killed, then the ScheduledExecutorService will get killed along with it. Instead use an AlarmManager
Here are 3 steps
Create the Alarm
Create the BroadcastReceiver
Register the Receiver in the App Manifest
1. Create the Alarm
public void createAlarm() {
//System request code
int DATA_FETCHER_RC = 123;
//Create an alarm manager
AlarmManager mAlarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
//Create the time of day you would like it to go off. Use a calendar
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
//Create an intent that points to the receiver. The system will notify the app about the current time, and send a broadcast to the app
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, DATA_FETCHER_RC,intent, PendingIntent.FLAG_UPDATE_CURRENT);
//initialize the alarm by using inexactrepeating. This allows the system to scheduler your alarm at the most efficient time around your
//set time, it is usually a few seconds off your requested time.
// you can also use setExact however this is not recommended. Use this only if it must be done then.
//Also set the interval using the AlarmManager constants
mAlarmManager.setInexactRepeating(AlarmManager.RTC,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pendingIntent);
}
2. Create the BroadcastReceiver
//This is the broadcast receiver you create where you place your logic once the alarm is run. Once the system realizes your alarm should be run, it will communicate to your app via the BroadcastReceiver. You must implement onReceive.
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//Your code once the alarm is set off goes here
//You can use an intent filter to filter the specified intent
}
}
3. In your app manifest register the receiver
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your.package">
//This is a useful permission as it allows your apps alarm to still be active once a reboot takes place
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<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=".MainActivity$AlarmReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>

Android alarm manager pending intents lost

This is my first post and I am new to Android App development so please bear with me.
I have written (and use every day) a pretty good reminders App because all the others were either too complicated or too simple. It is also on the play store, although not easy to find, but thats another story.
While I am happy with the whole App the alarms which show a notification or sound an alarm do not always happen. If the alarm is soon then it will but more than say 2 hours from now it often gets missed. Using "adb shell dumpsys alarm" it looks as if they are being discarded?
I have googled for days and have tried so many things, from this forum too, but no luck so I thought I would post some code to see if anybody can please tell me were I am going wrong as it should work as far as I can see.
So here goes.
I use alarm manager to queue the pending intent to call my broadcast receiver which starts a service which starts an activity which creates a notification and optionally sounds an alarm.
I was skipping the service bit but after googling it seems I should use a service. And as mentioned, this works fine for alarms happening soon but not after a few hours or a day or more. Could be due to Doze mode or my app being stopped by Android?
I would really appreciate some help please as I am pulling my hair out!!
Thanks in advance.
Here is my manifest. Where I think the problem is?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wingwares.reminders">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<supports-screens
android:anyDensity="true"
android:normalScreens="true" />
<application
android:icon="#drawable/reminder2"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<receiver android:name=".WidgetProvider">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/widget_info" />
</receiver>
<service
android:name=".WidgetService"
android:exported="false"
android:permission="android.permission.BIND_REMOTEVIEWS" />
<activity
android:name=".AppPreferences" />
<activity
android:name=".ListActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateAlwaysHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".RemActivity"
android:label="#string/add_reminder"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan" />
<service
android:name=".AlarmService"
android:enabled="true"
android:exported="true" >
</service>
<receiver
android:name=".AlarmReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="SET_ALERT" />
</intent-filter>
</receiver>
<activity
android:name=".AlarmDialog"
android:theme="#style/AlarmDialog" />
</application>
</manifest>
And my Alarm Manager from ListActivity
public void setAlert(Context context, boolean turnOn){
intent = new Intent(context, AlarmReceiver.class);
intent.putExtra("name", fullName());
intent.putExtra("notes", notes);
intent.putExtra("setalarm", setalarm);
intent.putExtra("millis", time.getMilliTime());
intent.putExtra("remId", remId);
pendingIntent = PendingIntent.getBroadcast(context, 8192 + remId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
if (turnOn) {
alertTime = time.getMilliTime() - (warning * 60000);
interval = getInterval();
//set repeating alarmManager just once
if (freq.equals(EVERY) && type.equals(FIXED) && date.equals(getOriginal()) && interval > 0) {
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alertTime, interval, pendingIntent);
} else {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alertTime, pendingIntent);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, alertTime, pendingIntent);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime, pendingIntent);
}
}
} else {
setalarm = false;
}
}
My broadcast receiver
public class AlarmReceiver extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent newIntent = new Intent(context, AlarmService.class);
//pass the extras on and show the dialog
newIntent.putExtras(intent);
startWakefulService(context, newIntent);
}
}
My Service
public class AlarmService extends IntentService {
public AlarmService (){
super("AlarmService");
}
#Override
protected void onHandleIntent(Intent intent) {
Intent newIntent = new Intent(this, AlarmDialog.class);
newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
//pass the extras on and show the dialog
newIntent.putExtras(intent);
startActivity(newIntent);
AlarmReceiver.completeWakefulIntent(newIntent);
}
}
My Activity (the notification part)
public class AlarmDialog extends Activity
implements DialogInterface.OnCancelListener, DialogInterface.OnClickListener {
Bundle extras;
int remId;
String name, notes;
Boolean setalarm;
long alarmTime;
MediaPlayer player;
PendingIntent pendingIntent;
SharedPreferences mPrefs;
NotificationManager manager;
Notification notification;
static final int SNOOZE_BUTTON = -2;
static final int DISMISS_BUTTON = -1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//create notification and alert box if alarmManager selected
mPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
extras = getIntent().getExtras();
alarmTime = extras.getLong("millis");
remId = extras.getInt("remId");
name = extras.getString("name");
notes = extras.getString("notes");
setalarm = extras.getBoolean("setalarm");
//notification first
notification = new RemNotification(this, name).get();
if (notification != null) {
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.cancel(remId);
manager.notify(remId, notification);
}
}

|Android 17| AlarmManager doesn´t start BroadcastReceiver

I am currently trying to call a BroadCastReceiver with an AlarmManager to ouput a notification on a certain time! On Android SDK Level 19+ my method works, but on Android 17 my approach leads into nothing!
There are no errors outputted, nor are there any logs in the monitor of Android Studio. The BroadcastReceiver is simply not called by the AlarmManager.
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mButton = (Button) findViewById(R.id.btn);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setNotification();
}
});
}
public void setNotification() {
Intent resultIntent = new Intent(this, NotificationPublisher.class);
resultIntent.setData(Uri.parse("notification://1995-" + System.currentTimeMillis()));
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
PendingIntent resultPendingIntent =
PendingIntent.getBroadcast(
this,
1995,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 30 * 1000, resultPendingIntent);
Log.d("Test", "Alarm started");
}
}
NotificationPublisher.java
public class NotificationPublisher extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "What", Toast.LENGTH_SHORT).show();
}
}
AndroidManifext.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="nbaye.apptesting">
<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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".NotificationPublisher"
android:process=":remote"
android:enabled="true" />
</application>
</manifest>
Min SDK: 17
Target: 23
EDIT:
I have looked into the AlarmManager with "adb shell dumpsys alarm":
The alarm is created by the app, but the "when" variable of the alarm is counting down from +30 seconds to an infinite negative number!

onReceive() function of BroadcastReceiver always called when device is rebooted

My program is implement BroadcastReceiver to setting Alarm. But I got issue : After setting time to alarm, I power off device. After that, I start device and onReceive() function of BroadcastReceiver always call. This is my code :
<receiver android:name="com.futaba.broadcastservice.AlarmBroadCastReceiver"
>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
How to fix this issue?
Have you read the documentation of BOOT_COMPLETED? It will run when the system is done booting.
Here's what you could do to reschedule your alarm after rebooting.
Code goes on your receiver.
public class AlarmBroadCastReceiver extends BroadcastReceiver {
Context ct;
#Override
public void onReceive(Context context, Intent intent)
{
ct=context;
if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
{
Intent contentIntent = new Intent(ct, AlarmBroadCastReceiver.class);
PendingIntent theappIntent = PendingIntent.getService(ct, 0,contentIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day, hour,minute);
AlarmManager am = (AlarmManager) ct.getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), theappIntent);
}
}
Just to be sure, you have to declare the uses-permission as follow
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name="com.futaba.broadcastservice.AlarmBroadCastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>

alarm does not show up

im trying to set a testalarm when i click on a button inside one of my fragments. but the alarm dosent show up.
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "OnReceive alarm test", Toast.LENGTH_SHORT).show();
}
}
//Button is inside a Fragment
Button SetAlarmButton = (Button)(v.findViewById(R.id.SetAlarmButton));
SetAlarmButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
try{
Calendar alarm = Calendar.getInstance();
alarm.set(Calendar.HOUR_OF_DAY, 18);
alarm.set(Calendar.MINUTE, 00);
long alarmMillis = alarm.getTimeInMillis();
Intent intent = new Intent(view.getContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(view.getContext(), 192837, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) view.getContext().getSystemService(view.getContext().ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, alarmMillis , pendingIntent);
}catch(Throwable e){AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
sw.toString();
builder.setMessage(sw.toString());
AlertDialog alert = builder.create();
alert.show();
}
}});
i know that i need to modify my manifest and add something like this but i dont know how and where exactly
<receiver android:name="AlarmReceiver">
....
</receiver>
to my manifest
<?xml version="1.0" encoding="UTF-8"?>
<manifest android:versionCode="1" android:versionName="1.0"
package="com.test.alarm" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="15"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application android:icon="#drawable/ic_launcher"
android:label="#string/app_name" android:theme="#style/AppTheme">
<activity android:label="#string/title_activity_main" android:name=".Main">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".First_Start"
/>
<activity
android:name=".Preferences"
android:label="Settings">
</activity>
</application>
</manifest>
Check out the docs for AlarmManager: http://developer.android.com/reference/android/app/AlarmManager.html
The Alarm that you're setting isn't related to the Alarm Clock functionality built in to the Clock application.
Alarms are used by the system to allow the application to respond to some event in the future. You basically tell the system you want your app to be woken up in some amount of time, and you define what Activity gets called when the Alarm goes off.
Intent i = new Intent(AlarmClock.ACTION_SET_ALARM);
i.putExtra(AlarmClock.EXTRA_MESSAGE, "New Alarm");
i.putExtra(AlarmClock.EXTRA_HOUR, hours);
i.putExtra(AlarmClock.EXTRA_MINUTES, mins);
startActivity(i);
but with this i cant specify the day i want the alarm to be set

Categories

Resources