Inform Activity from a BroadcastReceiver ONLY if it is in the foreground
So this is almost Bino's answer, but: instead of moving the receiver into the activity, use two receivers, with different Intents. The first one is your original alarm Intent, with a receiver registered in the manifest as you already have, and then that receiver sends a second broadcast intent, which is handled by a receiver registered by the activity as Bino says.
I've done this in my own timer project, on github. Here are the alarm receiver and the requery receiver. Hope that helps.
Can a Broadcast Receiver send information to an Activity only if it is active without starting a new Activity?
There are several different ways to accomplish the same task. One is registering a listener like the following example:
MainActivity
public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Receiver.setOnReceiveListener(new Receiver.OnReceiveListener() {
public void onReceive(Context Context, Intent intent)
{
//Do something.
}
});
}
@Override
protected void onDestroy()
{
super.onDestroy();
Receiver.setOnReceiveListener(null);
}
}
Receiver
public class Receiver extends BroadcastReceiver
{
private static OnReceiveListener static_listener;
public static abstract interface OnReceiveListener
{
public void onReceive(Context context, Intent intent);
}
public static void setOnReceiveListener(OnReceiveListener listener)
{
static_listener = listener;
}
@Override
public final void onReceive(Context context, Intent intent)
{
if(static_listener != null) {
static_listener.onReceive(context, intent);
}
}
}
Why BroadcastReceiver works even when app is in background ?
A BroadcastReceiver
works when the app is in the background because the event that the receiver picks up are sent globally, and each app is registered to listen in on these, regardless of whether or not it is running.
To deal with this, in your BroadcastReceiver
's onReceive
code, check if your app is in the foreground.
There is one--and only one that I know of--consistently effective method to do this. You need to keep track of your pause/resume actions for your application. Ensure that you check this in every activity.
There is some sample code in this answer (solution #1). In your case, you would want to check MyApplication.isActivityVisible() == true
as a validation before doing anything from your BroadcastReceiver
.
Broadcast receiver is not working when app on foreground or active
As I understood,
when you start application in first time, you see nothing, just service is started and a broadcast receiver is registered. When battery level will be changed, the method checkBatteryLevel() is calling and the broadcast receiver will be unregistered. As result you have never received a new changing of battery level.
How to know in BroadcastReceiver if App is running on foreground?
Try this way hope this works for you
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (isAppForground(context)) {
// App is in Foreground
} else {
// App is in Background
}
}
public boolean isAppForground(Context mContext) {
ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).topActivity;
if (!topActivity.getPackageName().equals(mContext.getPackageName())) {
return false;
}
}
return true;
}
}
Add this permission
<uses-permission android:name="android.permission.GET_TASKS" />
Related Topics
Android Httpsurlconnection Eofexception
How To: Install Plugin in Android Studio
Android Sqlitedatabase Query with Like
Calling a Activity Method from Broadcastreceiver in Android
Restarting a Device Programmatically
Install_Failed_Cpu_Abi_Incompatible on Device Using Intellij
How to Restrict My Edittext Input to Numerical (Possibly Decimal and Signed) Input
How to Import an Existing Project from Github into Android Studio
How to Embed Vlc Media Player to My Android App
Is Google-Services.JSON Safe from Hackers
How to Change Android:Windowsoftinputmode Value from Java Class
Remove Animation/Shifting Mode from Bottomnavigationview Android
Gradle Android Build for Different Processor Architectures
How to Get Pid of Android Application Without Using Adb Shell