Broadcast receiver distinguishing call reason based on action name of intent - android

I am having a problem with my application's broadcast receiver.
I have a situation where I wan the broadcast receiver to be called for 3 different reasons, first phone state changed, second, based on a timer, and third on boot complete. And then according to the reason by which the broadcast receiver was called I want to be able to do some stuff.
below is my android manifest of my receiver:
<receiver android:name="com.example.mobileraptor.MyPhoneListener" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="com.example.mobileraptor.TimerTriggered" />
</intent-filter>
</receiver>
and the following is where i declare my TimerTriggered intent:
PendingIntent pintent = PendingIntent.getBroadcast( this, 0, new Intent("com.example.mobileraptor.TimerTriggered"), 0 );
AlarmManager manager = (AlarmManager)(this.getSystemService( Context.ALARM_SERVICE ));
manager.setInexactRepeating( AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), AlarmManager.INTERVAL_HOUR , pintent );
The problem is that once the application wants to boot it crashes. but if I comment the line :
action android:name="com.example.mobileraptor.TimerTriggered
then my application is fine.
what is wrong with the action name that I have defined.

Related

setAlarmClock not work when set from reciever

In my code I have a broadcast reciever
<receiver
android:name=".alarm.AlarmReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.TIME_SET" />
</intent-filter>
<intent-filter>
<action android:name="com.BalDroid.YekNegah.alarm.dailyAction"
/>
</intent-filter>
</receiver>
I schedule alarm in this receiver in onReceive method
PendingIntent pi = PendingIntent.getBroadcast(context, AlarmRow.getId(mycursor), i, PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT >= 21) {
AlarmManager.AlarmClockInfo alarmInfo = new AlarmManager.AlarmClockInfo(
mycal.getTimeInMillis(),pi);
mgr.setAlarmClock(alarmInfo,pi);
// Create a Pending intent to show Alarm Details
}
although receiver run but alarm not trigger with mgr.setAlarmClock. But when I use
mgr.setExactAndAllowWhileIdle alarm trigger
(because of setAlarmClock accuracy I have to use setAlarmClock in my code)
And when I schedule mgr.setAlarmClock from MainActivity (when start application manually) it works.
I cant find the solution!!.
(I don't know why is not work from receiver).
My app also called setAlarmClock on AlarmManager, however the event didn't trigger. The reason for me was the default restriction of background processes by the manufacturer(my phone is Xiaomi).
Go to Settings -> Your application and find background restriction options. Turn the restrictions off and setAlarmClock must work.
As a conclusion, the result depends on manufacturer's default settings.

Use Alarm Manager to set Notification at specific time but my receiver class is not called on time

Long time = prefs.getLong("dailynoti", 0);
Intent intent = new Intent(cn, NotificationReceiver.class);
intent.putExtra("Desc", "Description...");
PendingIntent pendingIntent = PendingIntent.getBroadcast(cn, 1055, intent,0);
AlarmManager alarmManager = (AlarmManager) cn.getSystemService(cn.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,time, AlarmManager.INTERVAL_DAY, pendingIntent);
and my mainifest file
<receiver
android:name="com.mypackage.NotificationReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.REBOOT" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
And this same code is use in same project but now in same project another module it will not work Broadcast Receiver isn't call at given time
my notification code is in Receiver file but Receiver class isn't call.
I checked all things like time is also get properly. so what is the problem . please help...
I used many solution like
PendingIntent pendingIntent = PendingIntent.getBroadcast(cn, 1055, intent, PendingIntent.FLAG_CANCEL_CURRENT
//FLAG_NO_CREATE
//FLAG_UPDATE_CURRENT
One approach that I think that will work is instead of alarmManager.setRepeating() use alarmManager.set() and when the alarm triggers then set the next alarm. This will give you almost accurate results. Why almost? Because due to Doze mode introduced in Android M. So this will give you between 0 to +5 min delay.

How to stop a broadcast receiver from itself?

I have a BroadcastReceiver that will run on the background regardless of my application lifecycle. This BroadcastReceiver is initiated on the manifest and launched by a PendingIntent.
I need to stop this BroadcastReceiver at some point, and i must do it inside the BroadcastReceiver it self (inside onReceive perhaps?).
I tried to call abortBroadcast() but no luck, the BroadcastReceiver is still getting called again and again.
Please kindly help me, Thanks a lot for your help
UPDATE
This is how i launch the BroadcastReceiver :
Intent intent = new Intent("mypackage.ACTION_RECEIVE_GEOFENCE");
intent.putExtra("toStation", Constants.toStation);
PendingIntent geofencePendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
LocationServices.GeofencingApi.addGeofences(
mGoogleApiClient,
getGeofencingRequest(),
geofencePendingIntent
).setResultCallback(this);
And this is on my manifest :
<receiver android:name="mypackage.GeofenceUpdateReceiver"
android:exported="false">
<intent-filter >
<action android:name="mypackage.ACTION_RECEIVE_GEOFENCE"/>
</intent-filter>
</receiver>
I use Google's Geofence API

Broadcast receiver not exceuted on application force close

I have an application developed for jelly bean, where I schedule an event to be executed in the future using Alarm manager. The scheduled event executes as expected as long as the application runs in the foreground or in the background. But once I force close the application under taskmanager, I am no longer able to receive the broadcast from the alarm manager.
As suggested by various posts and blogs i tried using Intent.Flag_Include_Stopped_Packages. But it was of no use. Including this flag in the intent works only for sendBroadcast(intent). But in case of an alarm manager where pending intent is used,it does not work.
My code to schedule the alarm
Intent intent = new Intent("com.dummy.intent");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(),dummyId, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, scheduledAlarm.getTimeInMillis(), pi);
My mainfest
<receiver android:name="com.example.androidScheduling.alarmReceiver"
android:enabled="true"
android:exported="true"
android:process=":remote">
<intent-filter>
<action android:name="com.dummy.intent"></action>
</intent-filter>
</receiver>
Can someone please help me out?
I even tried including android:process = ":remote" for the receiver in manifest. But even that did not help.
I think you have not spelled correctly the intent's action name in manifest and programmatically .
In pro-grammatically
Intent intent = new Intent("com.dummy.intent");
Manifest file-
<intent-filter>
<action android:name="com.custom.intent"></action>
</intent-filter>
The action name to intent and declared in manifest must require same.
Hope this helpful to you

Broadcast Receiver and widget timezone

I use Broadcast Receiver, WidgetProvider and Configure Activity. Activity starts during the creation of a widget. Widgets are updated every 20 seconds.
private PendingIntent service = null;
Intent intent = new Intent(context, AlarmReceiver.class);
if (service == null) {
service = PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
}
m.setRepeating(AlarmManager.RTC, TIME.getTime().getTime(), 1000 * 20,
service);
I whant to update widgets when I change time or timezone on my phone. Widgets are updated when I change the time ahead. But when I change the time back, nothing happens.
I've already added to the manifest
<receiver android:name=".AlarmReceiver" >
<intent-filter>
<action android:name="android.intent.ACTION_TIMEZONE_CHANGED" />
<action android:name="android.intent.ACTION_TIME" />
</intent-filter>
</receiver>
In the Broadcast Receiver I'm updating widgets.
Please refer to
http://developer.android.com/reference/android/content/Intent.html#ACTION_TIMEZONE_CHANGED
They should be
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
<action android:name="android.intent.action.TIME_SET" />

Categories

Resources