How to check if my BroadcastReceiver receives the BOOT? - android

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>

Related

Why BroadcastReceiver not unregister when service destroy?

i register my (Broadcast Receiver) in service and when my main activity close my service destroid but my (Broadcast Receiver) Receiver working good?
and my question is how way to keep (Broadcast Receiver) in background?
and my solution is good idea ?
myservice code:
public class MyService extends Service{
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Const.LOCATION_LOGGER_ACTION);
intentFilter.addAction(Intent.ACTION_NEW_OUTGOING_CALL);
registerReceiver(new BroadcastReceivers(), intentFilter);
return START_STICKY;
}
}
When you create BroadcastReceiver in code (Activity or Service), BroadcastReceiver should be managed by activity or service itself.
Otherwise, if you declare Broadcast Receiver in manifest, if your application is not running, system automatically create new process for your receiver. This is extremely useful when your application doesn't available such as BootCompleted).
<receiver android:name=".CustomReceiver">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.MY_BROADCAST"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
onReceive check service is running
The previous answer was correct. Add the code from there to Your manifest file,
then create new java class:
public class MyReciever extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
//Do what you need to do when broadcast is received
}
}

Delay in Android connectivity change broadcast

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);
}
}
}

Are two Broadcast Receivers (one Alarm Manager and another for System Events) required?

I am calling the following BroadcastReceiver using AlarmManager every X mins. Its working fine.
public class MyAlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (CommonFuncs.isConnectedBySettings(context)) {
if(CommonFuncs.isUpdateTimeValid(context)){
Intent intentCurSvc = new Intent(context, CurrencySvc.class);
context.startService(intentCurSvc);
}
}
}
Manifest
<receiver
android:name=".service.AlarmSvcRecevier"
android:process=":remote" >
</receiver>
I want to use the same BroadcastReceiver for connectivity change as well, by just adding the following manifest.
<receiver
android:name=".service.AlarmSvcRecevier"
android:process=":remote" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
But my receiver is not getting called when WIFI is connected. I am not sure what is the problem. Do I need a separate receiver for AlarmManager and CONNECTIVITY_CHANGE to perform same operation? Is my approach correct?

BOOT_COMPLETE and ACTION_SHUTDOWN never call the BroadcastReceiver

I want to catch ACTION_SHUTDOWN and BOOT_COMPLETE using BroadcastReceiver.
But it turns out both signals never trigger the BroadcastReceiver (I didn't see any log on logcat).
Here is my source code.
I give the permission on Manifest
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
and I try to register the BroadcastReceiver in both ways
protected void onCreate(Bundle savedInstanceState)
{
registerReceiver(BootReceiver, new IntentFilter(Intent.ACTION_BOOT_COMPLETED));
registerReceiver(ShutDownReceiver, new IntentFilter(Intent.ACTION_SHUTDOWN));
}
<receiver android:name=".BootReceiver"
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>
and the source code for BootReceiver and ShutDownReceiver are as
private BroadcastReceiver BootReceiver = new BroadcastReceiver()
{
private String ACTION_BOOT = "android.intent.action.BOOT_COMPLETED";
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(ACTION_BOOT)){
//my stuff
Log.d("Power", "Boot Complete");
}
}
};
private BroadcastReceiver ShutDownReceiver = new BroadcastReceiver()
{
private String ACTION_SHUTDOWN = "android.intent.action.ACTION_SHUTDOWN";
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_SHUTDOWN)) {
//my stuff
Log.d("Power", "Shutdown Complete");
}
}
};
also, I unregister both BoradcastReceiver in onDestroy
public void onDestroy()
{
unregisterReceiver(BootReceiver);
unregisterReceiver(ShutDownReceiver);
super.onDestroy();
}
Does anyone know what's wrong with my code?
Or anything I miss? Thank you.
I found out why it didn't work.
Since I use a HTC device, the broadcast messages are different from others.
Shut down event broadcasts "com.htc.intent.action.QUICKBOOT_POWEROFF"
Restart(reboot) event broadcasts "android.intent.action.ACTION_SHUTDOWN"
Power on event broadcasts "com.htc.intent.action.QUICKBOOT_POWERON"
In other device, when shutting down the device, it might broadcast "android.intent.action.QUICKBOOT_POWEROFF".
Chances are that your application is not yet added to the "BOOT_COMPLETED" possible receivers list, starting from Android 3.1, in order to get the "BOOT_COMPLETED" action, your application must have been started explicitly by the user, either showing an Activity or another Component, until then your application will not receive the broadcast you are expecting, is important to know that if you "Force Close" the application, it will be missing the broadcasts again, So, try to open an activity and then reboot your device, you will get it...
Hope this Helps!
Regards!
Try this.
<receiver
android:name="packagename.GPSReceiver"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter android:priority="500" >
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
BOOT_COMPLETED must be registered in the manifest. You cannot register for it via registerReceiver(), because by the time you call registerReceiver(), the boot will have long since occurred.
AFAIK the shutdown broadcast works with registerReceiver(), though in your case it will only be around when your process is running.

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