Calling a Activity method from BroadcastReceiver in Android

try this code : your broadcastreceiver class for internet lost class : public class InternetLostReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { context.sendBroadcast(new Intent(“INTERNET_LOST”)); } } in your activity add this for calling broadcast: public class TestActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); registerReceiver(broadcastReceiver, new IntentFilter(“INTERNET_LOST”)); } BroadcastReceiver … Read more

Broadcast Receiver in kotlin

you can do it in the following way Create a broadcast receiver object in your activity class val broadCastReceiver = object : BroadcastReceiver() { override fun onReceive(contxt: Context?, intent: Intent?) { when (intent?.action) { BROADCAST_DEFAULT_ALBUM_CHANGED -> handleAlbumChanged() BROADCAST_CHANGE_TYPE_CHANGED -> handleChangeTypeChanged() } } } Register broadcast receiver in onCreate() function of your activity LocalBroadcastManager.getInstance(this) .registerReceiver(broadCastReceiver, IntentFilter(BROADCAST_DEFAULT_ALBUM_CHANGED)) … Read more

Detect Android app upgrade and set Application class boolean for show/hide of EULA

It’s much easier to just check your current app version. PackageInfo packageInfo = activity.getPackageManager() .getPackageInfo(activity.getPackageName(), 0); int versionCode = packageInfo.versionCode; When your app starts, you check your SharedPreferences for an integer value with the version code. If there is none, or if it doesn’t match, display the EULA. After the user accepts the EULA, write … Read more

java.lang.InstantiationException: class has no zero argument constructor

Your inner Broadcast receiver must be static ( to be registered through Manifest) OR Non-static broadcast receiver must be registered and unregistered inside the Parent class for this. I was using an Inner broadcast reciver, without registering it within the class. Either make it static and register in Manifest , or Make it non static … Read more

Android – how to unregister a receiver created in the manifest?

You can use the PackageManager to enable/disable a BroadcastReceiver in declared in the Manifest. The Broadcast Receiver will get fired only when it is enabled. Use this to create a Component ComponentName component = new ComponentName(context, MyReceiver.class); Check if the Component is enabled or disabled int status = context.getPackageManager().getComponentEnabledSetting(component); if(status == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) { Log.d(“receiver is … Read more

Get battery level before broadcast receiver responds for Intent.ACTION_BATTERY_CHANGED

This is how to get the battery level without registering a receiver: Intent batteryIntent = context.getApplicationContext().registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); int rawlevel = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); double scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); double level = -1; if (rawlevel >= 0 && scale > 0) { level = rawlevel / scale; } It can use a null BroadcastReceiver because of … Read more

android: broadcast receiver for screen on and screen off

The two actions for screen on and off are: android.intent.action.SCREEN_OFF android.intent.action.SCREEN_ON But if you register a receiver for these broadcasts in a manifest, then the receiver will not receive these broadcasts. For this problem, you have to create a long running service, which is registering a local broadcast receiver for these intents. If you do … Read more

Is there any way to receive a notification when the user powers off the device?

You can use the ACTION_SHUTDOWN Intent which is broadcast when the phone is about to shutdown. The documentation says: Apps will not normally need to handle this, since the foreground activity will be paused as well. In other words, if you respond to all the lifecycle events for your Activity appropriately, there’s no need to … Read more

Enable and disable a Broadcast Receiver

Well, what you basically have seems OK. I have the following code in one of my projects: boolean enabled=prefs.getBoolean(key, false); int flag=(enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : PackageManager.COMPONENT_ENABLED_STATE_DISABLED); ComponentName component=new ComponentName(EditPreferences.this, OnBootReceiver.class); getPackageManager() .setComponentEnabledSetting(component, flag, PackageManager.DONT_KILL_APP); I use this to disable a BOOT_COMPLETED receiver when I don’t have any active alarms that need to be scheduled. My … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)