android


Broadcast receiver not receiving


I want to make a broadcast and my Receiver should receive the broadcast, but it is not working.
I have the following code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="utilities.dip.com.checkbattlevelstackof">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<service
android:name=".YourService"
android:enabled="true"
android:exported="false"
android:label="#string/app_name" >
</service>
<!-- Receivers -->
<receiver
android:name=".AlarmReceiver"
android:enabled="true" />
<receiver
android:name=".BootReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<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>
</application>
</manifest>
The receiver is:
package utilities.dip.com.checkbattlevelstackof;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class BootReceiver extends BroadcastReceiver {
public static final String TAG = "BootReceiver";
public static final String ACTION_BOOT = "android.intent.action.BOOT_COMPLETED";
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(ACTION_BOOT)) {
// This intent action can only be set by the Android system after a boot
Log.d(TAG,"Received boot event");
Intent monitorIntent = new Intent(context, YourService.class);
monitorIntent.putExtra(YourService.HANDLE_REBOOT, true);
context.startService(monitorIntent);
}
else{
Log.d(TAG," Action received : " + intent.getAction());
}
}
}
When I am making a broadcast I am not getting any log:
platform-tools $ ./adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -c android.intent.category.DEFAULT -n utilities.dip.com.checkbattlevelstackof/utilities.dip.com.checkbattlevelstackof.BootReceiver
Broadcasting: Intent { act=android.intent.action.BOOT_COMPLETED cat=[android.intent.category.DEFAULT] cmp=utilities.dip.com.checkbattlevelstackof/.BootReceiver }
Broadcast completed: result=0
What is the mistake ?
That's because you have declared outside in your manifest. It basically should look like this:
<application
.....
<receiver
android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
Also while checking the action string especially for the default Android actions, don't use your own constant. Instead of using ACTION_BOOT constant use "Intent.ACTION_BOOT_COMPLETED"
Add receiver & service inside application tag.

Related Links

retrieve video (categoryID) from youtube using json
PreferenceFragment::onPreferenceTreeClick return value - what does it do?
ListView is not updating the entries from array
Point a computer to an Android phone - position or direction of the phone based on its sensors?
No Activity found to handle Intent - Error
multiple fragments from ONE ActionTab
update a DialogFragment view (and using a AsyncTask)
android send data to a .net webservice
Easy way to make an Android+Scala project with IntelliJ 12?
Intent not getting passed successfully! :(
Environment.getExternalStorageDirectory().getAbsolutePath() gives a different path - Android
Service getting called multiple times?
how to agregate progressdialog?
Getting navigation using google map Version 2 — Android
Resume download android
Problems Passing Intent Extras to same activity ANDROID

Categories

HOME
batch-file
cheerio
polymer
time-complexity
c99
nvd3.js
gpu
nunit
owl-carousel
javacc
spring-data-mongodb
cakephp-3.4
gembox-spreadsheet
jquery-ui
trac
flume
augeas
32bit-64bit
siddhi
mod-security
eip
flatpak
entitymanager
easyphp
google-data-studio
bourbon
boost-icl
charles
backpack-for-laravel
named-entity-recognition
dataflow-diagram
ios10.2
short-url
syntax-highlighting
8051
tizen-tv
carrierwave
python-2.6
record
reactjs.net
alphabetical
google-earth-engine
postgresql-8.4
parser-generator
naming
microsoft-metro
lpsolve
dapper-simplecrud
openresty
boxing
rackspace-cloud
gameplay-kit
change-tracking
vcloud-director-rest-api
rocks
custom-lists
winusb
observablecollection
clipboard.js
embedding
aurora
project-organization
yajsw
beanstalk
exim4
httpruntime.cache
user-management
crouton-os
state-restoration
character-replacement
sapi
terracotta
dllimport
rewrite
file-move
poppler
pax-exam
visual-sourcesafe-2005
rpart
batik
jsvc
nutiteq
mraid
osc
xmltype
gamekit
cross-join
appserver
imagefilter
winginx
syndicationfeed
accesscontrolexception
light
embedded-fonts
scrypt
windows-phone-8-sdk
ebay-lms
modular
tlf
route-provider
robotlegs
tree-balancing
faye
filemerge
exponentiation
timthumb
socketserver
openfaces
todos
technical-debt
soappy

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App