Alarmservice not working - android

In my application I need to run some code every midnight. This is the first time I am working on alarm service and Searched the internet for some snippets and examples.
Below is my code and its not working as expected, please help my pointing out my mistake:
SET A REPEATING ALARM
private static void initiateMidnightAlarm(Context context, AlarmManager alarmManager) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
PendingIntent midnightIntent = PendingIntent.getService(context, 0,
new Intent("com.alarms.MidnightProcess"), 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, midnightIntent);
}
DECLARE THE RECEIVER IN THE MANIFEST:
<receiver
android:name=".receivers.MidnightProcessReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="com.alarms.MidnightProcess"/>
</intent-filter>
</receiver>
RECEIVER CODE:
public class MidnightProcessReceiver extends BroadcastReceiver {
public MidnightProcessReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
processMidnightLogic(context);
}
public static void processMidnightLogic(Context context) {
Toast.makeText(context,"MidnightProcessReceiver",Toast.LENGTH_LONG).show();
}
}
UPDATE
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.appname">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<uses-permission android:name="android.permission.GET_TASKS"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<permission
android:name="com.appname.permission.C2D_MESSAGE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.appname.permission.C2D_MESSAGE"/>
<application
android:name=".BaseApplication"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
.
.
..
..
</activity>
<receiver
android:name=".receivers.MidnightProcessReceiver"
android:enabled="true">
<intent-filter>
<action android:name="com.alarms.MidnightProcess"/>
</intent-filter>
</receiver>
<receiver
android:name=".receivers.OnBootReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
</manifest>
Please help!
Thanks in advance.

Since you need to fire an implicit broadcast, rather than start a Service, you're retrieving the wrong type of PendingIntent. Change:
PendingIntent midnightIntent = PendingIntent.getService(context, 0,
new Intent("com.alarms.MidnightProcess"), 0);
to:
PendingIntent midnightIntent = PendingIntent.getBroadcast(context, 0,
new Intent("com.alarms.MidnightProcess"), 0);

Related

BroadcastReceiver not being called by Alarm(Manager)

Can you spot any reason as to why BroadcastReceiver is not being called when the Alarm goes off? If I have the alarm launch an explicit intent, it works just fine and my activity opens. If I set the intent to open my BroadCastReceiver then nothing happens so I think there may be something wrong with my receiver class or the Manifest. Here's how I setup the alarm:
Intent intent = new Intent(this, AlarmBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 324, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
mAlarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
Here's my broadcast receiver:
public class AlarmBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.e("MJH", "Alarm called...");
Toast.makeText(context, "Alarm...", Toast.LENGTH_LONG).show();
}
}
And here is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest package="mjh.com.apod"
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Required to act as a custom watch face. -->
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:name="android.support.multidex.MultiDexApplication"
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=".AlarmBroadcastReceiver"
android:enabled="true"
android:exported="true"
android:process=":remote">
</receiver>
</application>
Thank you so very much for your time.
I think you should be using PendingIntent.getBroadcast( instead of PendingIntent.getActivity(.
I had to call getBroadcast rather than getActivity to create the PendingIntent. That fixed it.

Alarm receive don't work

I'm making an alarm receiver, but the alarm is not triggered.
This is my code :
Start alarm in MainActivity :
private void setupAlarm(){
Intent intent = new Intent(this, com.logdata.AlarmReceiver.class);
PendingIntent pIntent = PendingIntent.getService(this, 0, intent, 0);
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime() + 1, 1000, pIntent);
Log.e("setupAlarm", "Setup alarm complete");
}
Manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.phonelogger"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.WRITE_CALL_LOG"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<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"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="SettingActivity" android:label="#string/action_settings" ></activity>
<service android:name="com.logdata.LogManager" />
<receiver
android:name="com.logdata.AlarmReceiver"
android:enabled="true">
</receiver>
</application>
AlarmReceiver :
package com.logdata;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class AlarmReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Intent myIntent = new Intent(context, com.logdata.LogManager.class);
context.startService(myIntent);
Log.e("AlarmReceiver", "Get message");
}
}
Can someone tell me what the problem is ?
Your Intent is used to explicitly start the AlarmReceiver class which is a BroadcastReceiver. Hence, you need to use getBroadcast(), not getService(), to create the PendingIntent object.
Replace
PendingIntent pIntent = PendingIntent.getService(this, 0, intent, 0);
with
PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
Try this. This will work.
Try To Use THis. It will Work for you
private void setupAlarm(){
Intent intent = new Intent(this, com.logdata.AlarmReceiver.class);
PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime() + 1, 1000, pIntent);
Log.e("setupAlarm", "Setup alarm complete");
}

Android 4.4.4 Kitkat: Broadcastreceiver not receiving on_boot_completed broadcast

My program is to listen receive_boot_complete broadcast when device completes the boot.
The program is working fine as expected with android 4.0.3, but the same thing is not happening when I am running the code on Android 4.4.4 device.
The broadcastreceiver is not receiving a boot completed broadcast.
Is there anything new for Android 4.4.4? Running it on Moto G.
My code is as below:
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.app"
android:versionCode="1"
android:installLocation="internalOnly"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.INTERNET" />
<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"
android:excludeFromRecents="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".MyService"
android:label="#string/ser_name"
android:icon="#drawable/serv_ico"
android:enabled="true" />
<receiver
android:name=".BootReceiver"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver
android:name=".AlarmReceiver"
android:enabled="true" >
</receiver>
</application>
BootReceiver
public class BootReceiver extends BroadcastReceiver{
#Override
public void onReceive(final Context context, Intent intent) {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
// Start alarm manager on boot
// Start service
}
}, 60000);
}
}
AlarmReceiver
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context context, Intent intent) {
// Start service
}
}
MainActivity
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Run service on application start
setContentView(R.layout.activity_main);
// Initialize alarm manager application start
PackageManager p = getPackageManager(); // Remove App icon from launcher
p.setComponentEnabledSetting(getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
}
}
AlarmManager
public class Alarm {
public Alarm schedule(Context context) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 20);
calendar.set(Calendar.MINUTE, 15);
calendar.set(Calendar.SECOND, 0);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pi);
return null;
}
}
You have to define your receiver with exported = true and enabled = true
<android:name=".BootReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" />
This might be because of a bug in Android 4.4.4 r1/r2
See
Bug in Android KitKat

AlarmManager Does not work when Activity in background

In my App, I'm using AlarmManager for transmitting keep-alive every day once a day.
I'm using the following code for starting athe AlaramManager
Calendar calendar = DateUtils
.getNextKeepAliveTime(keepAliveHours, keepAliveMinutes);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(),
ALARM_REQUEST_CODE, intent, 0); // PendingIntent.FLAG_UPDATE_CURRENT);
// Get the AlarmManager service
Log.d(TAG, "cal: " + calendar.toString());
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
long diff = calendar.getTimeInMillis() - System.currentTimeMillis();
if(diff < 0) diff = AlarmManager.INTERVAL_FIFTEEN_MINUTES;
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, diff, AlarmManager.INTERVAL_DAY, sender);
When activity goes background, the receiver, AlarmReceiver, does not get any event. But it working successfully when activity in foreground.
Ther receiver declaration in my manifest is -
<receiver android:name="MainActivity$AlarmReceiver" android:enabled="true" />
The manifest is
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.yachtdroid.ba.MainActivity"
android:label="#string/app_name"
android:launchMode="singleInstance" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.yachtdroid.ba.ui.Settings" >
</activity>
<receiver android:name="MainActivity$ChargingOnReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
</intent-filter>
</receiver>
<service android:name="com.yachtdroid.ba.services.MailSenderService" />
<service android:name="com.yachtdroid.ba.services.SmsSender" />
<activity android:name="com.yachtdroid.ba.ui.YDSiteView" />
<activity
android:name="com.yachtdroid.ba.ui.BatteryAlertDialog"
android:theme="#style/NoTitleDialog" />
<activity
android:name="com.yachtdroid.ba.ui.ActivityLog"
android:theme="#style/NoTitleDialog" />
<receiver android:name="MainActivity$AlarmReceiver" android:enabled="true" />
<!-- intent-filter>
<action android:name="alarm_action"/>
</intent-filter>
</receiver -->
</application>
The receiver code is
public static class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, intent.getExtras().toString());
if (!inForeground) {
Log.d(TAG, "*** Move app to front!");
Intent it = new Intent("intent.my.action");
it.setComponent(new ComponentName(context.getPackageName(),
MainActivity.class.getName()));
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.getApplicationContext().startActivity(it);
}
try {
Toast.makeText(context, "alarm message", Toast.LENGTH_SHORT).show();
MainActivity.instance.sendKeepALive();
} catch (Exception e) {
Toast.makeText(
context,
"There was an error somewhere, but we still received an alarm",
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
How it can be solved?
Thanks,
Eyal.
Try to remove the code for the broadcast receiver from the Activity ,make another java file and put it there and also modify your manifest as per that
<receiver android:name=".AlarmReceiver" />
instead of
<receiver android:name="MainActivity$AlarmReceiver" android:enabled="true" />

Android: Broadcast receiver won't receive boot up message

I've a class MyBroadcastReceiver that is registered to start at boot up. The manifest is below. I am running this via eclipse on a Android Motorola Xoom wifi only tablet - which is Android 3.2, API level 13 (from the dev.android site).
I have googled for this and tried a few things:
put android:exported="true"
used android:installLocation="internalOnly" to make sure it doesn't install on the SD card
after installing on the device, I ran the app a couple of times before testing the bootup (as some links said Android 3.x onwards won't immediately start sending boot events to the app unless user has manually initiated it at least once)
However even now the broadcast receiver doesn't seem to run when the device boots up (based on logcat).
Here is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mypack"
android:versionCode="1"
android:versionName="1.0"
android:installLocation="internalOnly">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-sdk android:minSdkVersion="8" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".MainCamApp" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".UploaderService" />
<receiver android:name=".MyBroadcastReceiver" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
</manifest>
Code for broadcast receiver -
import java.util.Calendar;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyBroadcastReceiverX extends BroadcastReceiver {
private String TAG = "Broadcast Receiver:";
AlarmManager alarm;
#Override
public void onReceive(Context arg0, Intent arg1) {
Log.i(TAG, "Entered onReceive.");
alarm = (AlarmManager) arg0.getSystemService(Context.ALARM_SERVICE);
Intent intentUploadService = new Intent (arg0, UploaderService.class);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
PendingIntent pi = PendingIntent.getBroadcast(arg0, 0, intentUploadService , 0);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5000, pi);
Log.i(TAG, "Alarm set. Exiting onReceive.");
Intent myIntent = new Intent(arg0, UploaderService.class);
arg0.startService(myIntent);
}
}
Seems to be working now.. final manifest below if it helps -
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0"
android:installLocation="internalOnly"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-sdk android:minSdkVersion="8" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".MainCamApp" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".UploaderService" />
<receiver android:name=".MyBroadcastReceiverX" android:enabled="true" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
<category android:name="android.intent.category.DEFAULT"/>
</action>
</intent-filter>
</receiver>
</application>
</manifest>
Try the following:
<receiver android:name=".MyBroadcastReceiverX" android:enabled="true" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
<category android:name="android.intent.category.DEFAULT"/>
</action>
</intent-filter>
</receiver>
You also have incorrect class name specified in manifest - it should be MyBroadcastReceiverX rather then MyBroadcastReceiver
You need to write code after receiving the android.intent.action.BOOT_COMPLETED intent.
try this.
#Override
public void onReceive(Context context, Intent intent) {
Log.d("TEST", intent.getAction());
if (intent.getAction().equalsIgnoreCase(
"android.intent.action.BOOT_COMPLETED")) {
Log.i(TAG, "Entered onReceive.");
alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intentUploadService = new Intent (context, UploaderService.class);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intentUploadService , 0);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5000, pi);
Log.i(TAG, "Alarm set. Exiting onReceive.");
//no need to initialize intent twice
// Intent myIntent = new Intent(context, UploaderService.class);
context.startService(intentUploadService );
}
}

Categories

Resources