Delay in Android connectivity change broadcast - android

I am listening for "android.net.conn.CONNECTIVITY_CHANGE" in my app with a broadcast receiver. It does work, but the only problem I have is that there is a huge delay in receiving this broadcast. It takes between 1-2 seconds for the app to receive this broadcast. Am I doing something wrong?
My Manifest entry -
<receiver android:name=".ConnectivityBroadcastReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
and ConnectivityBroadcastReceiver class -
public class ConnectivityBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.e(TAG,"Broadcast received");
final String action = intent.getAction();
if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
ConnectivityUtil.checkConnectivity(context);
}
}
}

Related

SMS_RECEIVED BroadcastReceiver does not fire when App process is inactive

I have registered a Broadcast receiver to listen to incoming SMS messages. I have observed that the Broadcast receiver does not fire, if my App process is not active.
AndroidManifest.xml
<receiver android:name=".SmsBroadcastReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
SmsBroadcastReceiver.java
public class SmsBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = SmsBroadcastReceiver.class.getSimpleName();
#Override
public void onReceive(Context context, Intent intent) {
Log.e(TAG, "New SMS-message received");
}
}
}
I have put Log messages on App's onCreate callback and I confirmed that App's onCreate is also not being called when SMS'es are received on the device.
Android Version - 6.0.1
Device - OnePlus 2

Android After Reboot Broadcast Receiver is not running

I used this permission:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
and the receiver is:
<receiver android:name=".auth.NotificationBroadcast" android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
and receiver in code is:
#Override
public void onReceive(Context context, Intent intent) {
System.out.println("BroadcastReceiverBroadcast--------------------ReceiverBroadcastReceiverBroadcastReceiver----------------BroadcastReceiver");
if (intent != null) {
String action = intent.getAction();
switch (action) {
case Intent.ACTION_BOOT_COMPLETED:
System.out.println("Called on REBOOT");
// start a new service and repeat using alarm manager
break;
default:
break;
}
}
}
After reboot it's still not getting called in lollipop, but on marshmallow it's running.
try to put this line in your receiver's intent-filter.
<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
If your application is installed on the SD card, you should register this to get the android.intent.action.BOOT_COMPLETED event.
Updated: Since your app is using alarm service, it should not be installed on external storage. Reference: http://developer.android.com/guide/topics/data/install-location.html
Whenever the platform boot is completed, an intent with android.intent.action.BOOT_COMPLETED action is broadcasted. You need to register your application to receive this intent. For registering add this to your AndroidManifest.xml
<receiver android:name=".ServiceManager">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
So you will have ServiceManager as broadcast receiver to receive the intent for boot event. The ServiceManager class shall be as follows:
public class ServiceManager extends BroadcastReceiver {
Context mContext;
private final String BOOT_ACTION = "android.intent.action.BOOT_COMPLETED";
#Override
public void onReceive(Context context, Intent intent) {
// All registered broadcasts are received by this
mContext = context;
String action = intent.getAction();
if (action.equalsIgnoreCase(BOOT_ACTION)) {
//check for boot complete event & start your service
startService();
}
}
private void startService() {
//here, you will start your service
Intent mServiceIntent = new Intent();
mServiceIntent.setAction("com.bootservice.test.DataService");
mContext.startService(mServiceIntent);
}
}
Since we are starting the Service, it too must be mentioned in AndroidManifest:
<service android:name=".LocationService">
<intent-filter>
<action android:name="com.bootservice.test.DataService"/>
</intent-filter>
</service>

Alarm Manager after shutdown

When I tried this code, everything is working fine except, service is not getting start after device reboot. I want to start same service automatically. I am testing this example by connecting mobile with USB. what do I need to change ?
[http://javatechig.com/android/repeat-alarm-example-in-android]
try like this
<!-- for reboot event to reset alarms -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
then
<receiver
android:name="com.yourapp.receiver.RestartAppReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
next you have to create the BroadcastReceiver class
public class RestartAppReceiver extends BroadcastReceiver {
private static final String LOG_TAG = "RestartAppReceiver";
public RestartAppReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
String action = intent.getAction();
switch (action) {
case Intent.ACTION_BOOT_COMPLETED:
Log.i(LOG_TAG, "Start resetting alarms after reboot");
//restart what you need
Log.i(LOG_TAG, "Finish resetting alarms after reboot");
break;
default:
break;
}
}
}
}
You have to create a broadcast receiver that will listen for boot complete event and when that event is received start your service again
<receiver android:name=".Autostart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
create a class like this and add your code in onReceive method
public class Autostart extends BroadcastReceiver
{
public void onReceive(Context arg0, Intent arg1)
{
Log.i("Autostart", "**********started************");
}
}

How to check if my BroadcastReceiver receives the BOOT?

I have a BroadcastReceiver who should receive the BOOT.
Is there a simple way to check if the BroadcastReceiver receives the BOOT correctly?
Thanks
Assuming BOOT means BOOT_COMPLETED then you could use the following:
public class BootCompleteReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
//Do a Log or, less likely, a Toast or start your application here.
}
}
};
You would register this in the manifest like such:
<receiver android:name="com.example.yourAppName.BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>

Broadcast receiver not triggered after app is stopped

I have a broadcast receiver defined in the manifest with the android.bluetooth.device.action.ACL_CONNECTED in the intent filter.
It's triggered fine when the app is in the stack, but after I stop it from android settings it won't trigger anymore. any suggestions?
Update for Menny:
<receiver android:name=".auto.AppLauncher">
<intent-filter>
<action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
</intent-filter>
</receiver>
Did you program a BroadcastReceiver in your Code?
public class receiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(BluetoothDevice.ACTION_ACL_CONNECTED)) {
//something you want to do here
}
}
}

Categories

Resources