Receiving connection change on Android with a huge delay - android

I have both CONNECTIVITY_CHANGE & STATE_CHANGE set in my manifest
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.STATE_CHANGE" />
but when i turn wifi on/off manually it take sometimes up to 1 minute before my receiver receives the information, even though I can see that the wifi is back (other app can resume their actions)
Changing the intent-filter priority doesn't change anything, event when setting MAX_INTEGER as a priority.
Any idea how to get this information faster ?

Related

Monitor WiFi sleep mode events

I have to monitor the connectivity of the device automatically, to do that I have my receiver registered with
<receiver android:name=".NetworkStateReceiver" android:exported="true">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
<action android:name="android.net.wifi.STATE_CHANGE"/>
<action android:name="android.net.wifi.supplicant.CONNECTION_CHANGE" />
</intent-filter>
</receiver>
It works fine, but when the WiFi goes to sleep mode, the network info will have the state DISCONNECTED/BLOCKED. This is ok, I understand why it does this to save battery etc.
The issue is that the receiver doesn't receive an intent when the WiFi goes to sleep mode. Same thing if I wake up the device, the WiFi will switch to connected state but I won't receive an intent.
(Tested on a Nexus 5X)
Any way I could receive those events without having to use an alarm that would fire every X minutes to check the state?
Checked Connectivity Change Broadcast receiver not triggering when phone is in sleep mode but answer seems irrelevant

I'm trying to figure out how to run a thread or activity in the background but still allow user's to use other app's

I'm trying to make an app for work that acts as parental control's and control's the wifi state, gps state, mobile state, and 4G state. I need it to run in the background to check the state's every 60 sec and then change it if needed. Also it needs to start doing this as soon as the device turns on. My question is how do I run it in the background but still allow the user to use one other app? (yes i know this would most likly drain battery life.)
The thing what you are looking for is called Services.
They offer the functionality which you are looking for. You can also start a service with the phone bootup and interact with them via IPC.
Register a BroadcastReceiver to start your application just after the device turns on
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
...
<receiver
android:name="com.package.YourReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
In your BroadcastReceiver, do whatever initialization you need and then configure the AlarmManager to be notified periodically, ideally on a WakefulBroadcastReceiver
Every time you are notified, start a IntentService and do your work on there.

Go SMS Pro overriding android.provider.Telephony.SMS_RECEIVED

I have an app that allows users to forward SMS messages as they are received. Back in the day, I had the android:priority in my intent-filter for android.provider.Telephony.SMS_RECEIVED set to barely above Android's default messaging app. I did so, so that the user can forward the messages without having to clear them in the messaging app. However, a few months later, Go SMS Pro released an update of their app with a android:priority value extremely high, so users with that app installed started having issues. I updated my app so that my priority was slightly higher than Go SMS Pro's and everything went fine. Now Go SMS Pro did it again, their android:priority has been taken up to the max value you can assign. 2147483647 on one of their broadcast receivers and 2147483640 on another (yes, Go SMS Pro has two broadcast receivers now).
To fix this, I tried changing my priority to max just like theirs, but somehow their app keeps wining the SMS fight even though our priorities are the same. I read somewhere that Android when having to decide between two apps with the same priority, it chooses the one installed the earliest. I tried installing Go SMS Pro AFTER my app, but still nothing.
What's the proper way to fix this? What are they doing in their app that makes them get the SMS broadcast before anyone else, even with same android:priority values?
What's the proper way to fix this?
Modify your app to survive being run after Go SMS Pro or any other app. You might also advise users if you detect apps monitoring the same broadcast with equal-or-higher priorities (use PackageManager for this), so they know to configure those apps accordingly.
Trying to have a higher priority than everyone else is an arms race, one in which you will eventually wind up, at best, tied with the same priority. The behavior of ordered broadcasts with tied priorities is undocumented, and therefore there is no guaranteed behavior. Android -- or customized versions of Android -- are welcome to modify the tie-breaker algorithm. That algorithm could be anything from:
alphabetical order by package name
order as found in a hashed collection, where the hash key could be based on anything (e.g., object ID)
random number generator
Since you cannot reliably "win", modify your app to be as successful as possible when you do indeed "lose".
go sms pro has set these lines in it's manifest for SmsReceiver:
<receiver android:name=".smspopup.SmsReceiver" android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.GSM_SMS_RECEIVED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="android.provider.Telephony.WAP_PUSH_RECEIVED" />
<data android:mimeType="application/vnd.wap.mms-message" />
</intent-filter>
<intent-filter>
<action android:name="com.android.mms.transaction.MESSAGE_SENT" />
</intent-filter>
</receiver>
all these intent-filters make its priority higher than your receiver even if your reciever has the priority set to 2147483647. you can see the list of all receivers of all apps by:
List<ResolveInfo> receivers = getPackageManager().queryBroadcastReceivers(new Intent("android.provider.Telephony.SMS_RECEIVED"), 0);
the first receiver in the list, receives the sms before than others

Does android:exported="false" restrict system calls/access?

I know that setting android:exported="false" will restrict other applications from using/accessing my application components.
Does setting this attribute will restrict the system as well from using/accessing my components? i doubt.
for example, i have the following receiver:
<receiver
android:name="ConnectivityManager"
android:label="NetworkConnection"
android:exported = "false" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
The receiver is still receiving broadcasts from the system once the connection state changes (on/of).
Does this mean that the system calls/access can never be stopped? Please illustrate.
Google‘s online document does not describe this clearly.
From the real result, the receiver can still be triggered by system broadcast even if set exported="false".
Yes, if exported="false", it will still respond to system broadcasts such as connectivity changes. This is easy to test by using your above broadcast-receiver and toggling airplane mode

handling phone shutdown event in android

Could you post a simple code to handle shutdown event.
I need manifest and receiver.
I need the following:
I have a service running in background and I want to detect phone shutting down in order to make it in silent mode so that the terrible operator music doesn't play.
After restarting the phone (I already have on boot receiver) the service will enable phone sound.
You need to listen for the following intent action in your receiver:
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
To make it compatible with some HTC (or other) devices which offer quick power-off feature, you need to include the following action too with in the same intent-filter:
<action android:name="android.intent.action.QUICKBOOT_POWEROFF" />
For more info, read this
<receiver android:name=".ShutdownReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
<action android:name="android.intent.action.QUICKBOOT_POWEROFF" />
</intent-filter>
</receiver>
public static final String ACTION_SHUTDOWN
Broadcast Action: Device is shutting down. This is broadcast when the device is being shut down (completely turned off, not sleeping). Once the broadcast is complete, the final shutdown will proceed and all unsaved data lost. Apps will not normally need to handle this, since the foreground activity will be paused as well.
This is a protected intent that can only be sent by the system.
Constant Value: "android.intent.action.ACTION_SHUTDOWN"
And this ACTION_SHUTDOWN may differ from device to device
Like HTC its android.intent.action.QUICKBOOT_POWEROFF

Categories

Resources