I need to listen for Install, Updates and Remove broadcasts via a BroadcastReceivers, here is the definition:
<receiver android:name=".InstallReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package"/>
</intent-filter>
</receiver>
Now, Is it possible to add Intent.FLAG_RECEIVER_FOREGROUND flags to these incoming intents? because in android KitKat receivers affect to foreground services due to bug which reported here.
Any ideas on how to add this flag to incoming broadcasts?
While you are welcome to call addFlags() on the Intent, it will not have any effect. Only the flags added by the broadcaster matter, and you are not the broadcaster.
Related
i created an app that always start a service once the device has completed booting.
This is the Manifest code..
<receiver>
android:name="StartMyServiceAtBootReceiver"
android:directBootAware ="true"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
<action android:name="android.intent.action.REBOOT"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
The service always start in some devices once boot is completed but in my own device which is running android 7.0, the boot completed service doesn't start unless i convert the app to system app using root access. Can someone tell me how to make the app to start the service on boot completed without converting it to a system app?
First of All
receiver android:name="StartMyServiceAtBootReceiver"
Your path to Receiver should written over here for example:
<receiver android:name=".receiver.BootReceiver">
first dot suggest your path till root of the package.
Create a BroadcastReceiver and register it to receive ACTION_BOOT_COMPLETED. You also need RECEIVE_BOOT_COMPLETED permission.
Read: Listening For and Broadcasting Global Messages , and Setting Alarms
Try into manifest
<receiver
android:name=".AutoStart"
android:enabled="true"
android:exported="true" >
<intent-filter android:priority="500" >
<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>
Make sure also to include the completed boot permission.
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
Also read this answer carefully
First, I created a BroadcastReceiver with empty body. Then, I added it to the AndroidManifest.xml. But I found out that BroadcastReceiver declared in manifest does not receive any broadcast. I sent broadcast by
sendOrderedBroadcast(new Intent("com.example.action"), null)
or
adb shell am broadcast -a com.example.action
Both methods works on Android 7 but it does not work on Android 8. However, if the BroadcastReceiver is declared through registerReceiver, then it can still receive the broadcast.
On the other hand, android.hardware.usb.action.USB_DEVICE_ATTACHED works fine on both Android 7 and 8.
I want to ask why does it happen? I have tested it in both emulator and a physical device. They have the same behavior.
AndroidManifest.xml
...
<receiver
android:name=".device.UsbBroadcastReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
<action android:name="com.example.action" />
</intent-filter>
<meta-data
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="#xml/device_filter" />
</receiver>
...
As part of the Android 8.0 (API level 26) Background Execution Limits, apps that target the API level 26 or higher can no longer register broadcast receivers for implicit broadcasts in their manifest.
Read this
Manifest Based broadcast working for me in android 8
Broadcast restriction by android is not applicable for all the broadcasts some can still be registered inside manifest . But do check latest documentation since these may way depending on android version .
<receiver
android:name=".UsbReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
</intent-filter>
<meta-data
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="#xml/device_filter" />
I want to handle received sms that contains network configurations(OMA OTA). I have already developed android application that catches typical sms from inbox. Network configuration is not a typical sms. How can I get it? What is a Broadcast Action for that?
I was trying this:
<receiver android:name="ru.tenet.pdureceiver.SMSReceiver" >
<intent-filter android:priority="1" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
As I understood, android.provider.Telephony.SMS_RECEIVED is not enough.
Well, I've found a solution. The action is:
android.provider.Telephony.WAP_PUSH_RECEIVED
And also there must be a mimeType defined for wap message.
For example:
<intent-filter android:priority="1" >
<action android:name="android.provider.Telephony.WAP_PUSH_RECEIVED" />
<data android:mimeType="application/vnd.wap.connectivity-wbxml" />
</intent-filter>
is it possible to keep track of install/uninstall actions of Applications on the Android system? I was not able to find a proper Intent Action or Broadcast event so far.
What I'm searching for an observer for applications like there are observers for Calendar or CallLog changes. Does there exists an CONTENT_URI or Broadcast event?
Regards
You can create a broadcast receiver for package install and unistall.
<receiver android:name=".YourReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_INSTALL" />
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package"/>
</intent-filter>
</receiver>
For More info go to link
I have something like this in my manifest file - I need one receiver for the situation that a power source was connected and the other receiver for unplugged source.
<receiver android:name=".PowerConnectionOnReceiver" >
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
</intent-filter>
</receiver>
<receiver android:name=".PowerConnectionOffReceiver" >
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
</intent-filter>
</receiver>
But I want to unregisterReceiver, when onPause is called in my MainActivity. How to do it?
If your broadcast receiver is specified in the manifest, it cannot be unregistered programmatically. You will need to take it out of the manifest and register it from within your code.
You cannot unregister these receivers because they are defined in manifest, not in code. Receivers which are defined programmatically are the one which may get unregistered.