I have an Android app that listens to SMS messages. This is in the manifest:
<receiver android:name=".IncomingSMSBroadcastReceiver" android:enabled="true">
<intent-filter android:priority="100">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
The broadcast receivers works fine if the app is installed and opened. But I want the receiver to be able to be called immediately after install, in case user has remotely installed from the Android Market website and is not present in front of the phone. When I install APK using adb
./adb install myapp.apk
I can see the app is installed. But when I send a SMS message, my app doesnt respond. After I open the app once, it appears all the initialization happens at this point, and now the app responds to SMS messages just fine.
One possibility is that the ADB install is different from the Market install.
Any ideas?
It is not Possible. AFAIK until receiver is not registered then it can not detect any action.You have not any problem with code.But your receiver will work when it start one.For this we have to run our application at least one time.So that Manifest Register the Receiver
Related
I have implemented RECEIVE_BOOT_COMPLETED by making an entry in manifest and implementing MyBootReceiver.onReceive(..).
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
....
<receiver
android:name=".MyBootReceiver"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
</intent-filter>
</receiver>
It works well when run on an emulator (Pixel 2 API 21). However, when it is run on a API Level 30 or on PHysical device, OnePlus 6 (Oxygen 10.3.7), the BOOT notification isn't received.
But, when phone is restarted, other applications like Whatsapp, Sms etc are able to receive messages from server, possibly using some notification event. How is that possible ?
Am I missing something ?
one plus and Mi have customized their OS on top of Android. So they have overriden this function. refer Broadcast Receiver Not Working After Device Reboot in Android
On newer Android versions, you need to run adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -p com.mypackage.name. Without restricting the broadcast to your app, your device will actually reboot
regd your question,
But, when phone is restarted, other applications like Whatsapp, Sms
etc are able to receive messages from server, possibly using some
notification event. How is that possible ?
It's also possible by accessing the Phone Network State
I was expecting my app to load the BroadcastReceiver AutoStartOnBoot when I reboot my device.
I uninstall and install the app. Which means that all existing settings are deleted. I then power down the phone. And power it back up, the Broadcast receiver is never called.
I now, power down the device one more time and power it up again. Yet, broadcast receiver is not called.
I now launch the app once. Clear data. And power it down. I power it up. Now, the broadcast receiver is called.
Manifest
<receiver
android:name=".AutoStartOnBoot"
android:enabled="true"
android:exported="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" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
I have the permission setup
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Edit:
From your First two Points
1.I uninstall and install the app. Which means that all existing settings are deleted. I then power down the phone. And power it back up, the Broadcast receiver is never called.
2.I now, power down the device one more time and power it up again. Yet, broadcast receiver is not called.
Why it is not working??
Here your just installed the app but not launched.In android after first launch only all your manifest data is registered and all receivers work.But in your third case its working because you are launched the app so here all receivers gets registered.
For more check here Broadcast Receiver not working immediately after package installation
You have to add this permission outside of the <application> tag in manifest file
instead of this
<android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
Add this like
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
The behavior that you describe is perfectly normal. Something needs to use an explicit Intent to start up one of your components before your manifest-registered receivers will work. Typically, that will be the user tapping on your home screen launcher icon.
See the Android 3.1 release notes for more about this.
All answers are correct. This behavior is as per expectation. The app is inactive when installed, until its manually launched by the owner. Only after that is the BOOT_COMPLETED broadcast receiver registered to the OS.
Unless we put the app in the system folder, which keeps all apps in active state. We are device company, adb push your.apk /system/app is possible for us.
Some interesting links, here and here
I am trying to receive SMS from my android app. I have following receiver specified in my manifest.
<receiver android:exported="true" android:name="com.lahiruchandima.myapp.SMSReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
SMSReceiver successfully receives SMS if my app is installed and opened once (app does not need to be running at the moment the SMS is received to the device). But, if i do not open my app at least once after a fresh install, it doesn't receive any SMS.
Does anybody know a way to make it possible to receive SMS without opening the app at least once?
Does anybody know a way to make it possible to receive SMS without opening the app at least once?
It can't be done, on newer Android versions at least. Ever since Android 3.1, apps are installed in a stopped state, and require that the user open it at least once before components like your BroadcastReceiver can function. This is for security reasons, to prevent, or at least hamper, malicious program behavior.
I've recently added GCM messaging to my app using google's helper classes (GCMBroadcastReceiver, GCMBaseIntentService). It works beautifully when the app is running, both when it's in the foreground and when it's not. However, when it's not running, nothing works.
As a test, I extended GCMBroadcastReceiver and added log statements to getGCMIntentServiceClassName() and peekService(). When the app is running and a message arrives I see the former called. The OS then instantiates my service class, which eventually results in onMessage() being called.
When the app isn't running getGCMIntentServiceClassName() never gets called.
My manifest is pretty much the boiler-plate code from Google's GCM examples.
Is there an extra permission or flag I need to set in order for the OS to wake up my app when it's not running and a message arrives w/ the correct intent category? My receiver is defined as:
<receiver
android:name=".GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="PACKAGENAME" />
</intent-filter>
</receiver>
Bear in mind: this works when the app is running in the background.
Bleh. Figured it out. David Wasser's answer here:
BroadcastReceiver isn't working
explains why I'm not seeing the broadcasts when my app isn't running. I was force quitting it from Manage Applications, which puts it into the "stopped" state (and thereby causes the system to exclude it from broadcasts by default).
When I install the app, launch it, power down the device, then power it on again, I'm receiving the broadcasts properly.
I had same issue and fixed by just running the through RUN button from Android Studio.
I think in Debug Mode it doesn't work
Is there a way to detect from app code when it is being reinstalled.
I saw that app update from market can be detected by listening to PACKAGE_REPLACED event in broadcast receiver. But that these events are not delivered to the app , if the app is reinstalled from editor (eclipse ).
My requirement is that i am disabling a component(Launcher activity). The app install will fail if it is not enabled. SO everytime before reinstall i want to enable this component.
I am talking about the reinstall before publishing in market. While developing , each time i reinstall the app to test some modification, i want to detect this from my app and make the component enabled.
I saw from my example that the following events will be broadcasted when an app is reinstalled from eclipse.
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
</intent-filter>
I defined a broadcast receiver for these actions and from onreceive i am able to enable my component