Check if Activity is Open Before Displaying Push Notifications - android

Hello I have successfully been able to implement push notifications in my app however I have a slight issue I would like to fix. I have a NotificationsActivity that displays a list of all notifications received on the app.
Now if this activity is open there is simply no need for me to display push notifications at the top. So I would like a way to check if this activity is open before doing a push notification.
Is this possible? If yes how can it be done?

Yes it's possible. You will have to use the ActivityManager.
private boolean isActivityForeground( Class activityClass ) {
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.AppTask> tasks = activityManager.getAppTasks();
return activityClass.getName().equals( tasks.get(0).getTaskInfo().topActivity.getClassName() );
}

Related

Android: How to know what application is opened to foreground and do something when it goes to background

The title is bad, sorry. Could figure out how to say this in short.
So, I'm developing an application for Android which will only be used in my own couple tablets which are going to public use.
I need a service running in the background (or so I currently believe, prove me wrong if possible) which keeps track which application is foreground, and when that application goes to background the service starts activity which will go foreground and show something to the user(for example like some ad, or some review window which asks for start rating).
How could this be done?
Thank you.
E: The other applications can be 3rd party, so I can't modify the code of those.
When u start other activity at that time you current activity is OnPause() state and other activity goes onCreate() State.
You Must refer Activity LifeCycle for this.
You have to use Accessibility service. Starting from Android Lollipop you can't gather this kind of information. The only way is via accessibility docs.
to check other running tasks you should use ActivityManager.
ActivityManager acm = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> Ru_tasks = acm.getRunningTasks(NUMBER_OF_TASKS_U_WANT);
if (!Ru_tasks .isEmpty()) {
LOOP FOR EACH TASK (i is counter)
ComponentName topActivity = tasks.get(i).topActivity; // Or replace .topactivity with task name
if (topActivity.getPackageName().equals(WHAT_YOU LOOK FOR)) {
//Do WHAT YOU WANT!
}
}
get GET_TASKS permission

On push notification show an activity/popup, instead of the message in the status bar

I am an android newbie and i really need your help .On push notification (GCMintentservice) i get to do stuff using pending intent. but I want to show a popup or an activity instead of a push notification message in status bar. That is if the app is running the user will have an activity displayed and NOT a message on status bar. I know its not recommended. But is that possible? Any help is appreciated.
Thanks in advance
Yes! it is possible. On receiving push message from GCM, use following method:
public boolean isForeground(String myPackage) {
ActivityManager manager = (ActivityManager) ctx
.getSystemService(Activity.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> runningTaskInfo = manager
.getRunningTasks(1);
ComponentName componentInfo = runningTaskInfo.get(0).topActivity;
if (componentInfo.getPackageName().equals(myPackage))
return true;
return false;
}
You will come to know if your app is foreground or background. If it is background, show a notification else show an AlertDialog.
Above method needs this permission:
<uses-permission android:name="android.permission.GET_TASKS" />

How to notify when thirdparty application gets called

When third party apps in mobiles gets called i would like to notify user in a text message ..I am creating an app that will notify the user when i tries to open an application .
Help me to receive intent when ever app is tries to open..else sugges me some steps to find when the app icon is being clicked..
you can start a service, and execute every single minutes. Use ActivityManager to get the top activity in current running task. Something like this:
ActivityManager mActivityManager = (ActivityManager) context.getSystemService("activity");
ComponentName topActivity = mActivityManager.getRunningTasks(1).get(0).topActivity;
And then, you can do anything you want. Hope useful to u.
Don't forget the permission:
<uses-permission android:name="android.permission.GET_TASKS"/>

Creating start stop service button

I want to create a single button which serves both functions of Starting and Stopping a service.
Also I want to make sure that even if user quits the application and again comes back then according to whether service is running or not, I want to show appropriate Text on Button.
So in short, With what thing I can get to know if service is running or not ?
You can check if service is running or not with below code. Rest of the question is logic based once you find the service is running or not.
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if ("com.example.MyService".equals(service.service.getClassName())) {
return true;
}
}
return false;
}
I refer this answer just check it

Android Start Service on run?

what is the best way to implements a service from starting with alarm manager when android applications first starts?
Right now i have a Home activity which is the first page of the activity which runs the service when it is being created. However when the users were to start the application again on the app menu (when the application is running.), the home activity will be run again and the service will start again.
Is there any better alternative for this way of application?
Follow this link... have a look at the implemenation below.
http://blog.gregfiumara.com/archives/82 ...
and for the checking whether a service is active or not... you can simply use this.
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if ("com.example.background.BootableService".equals(service.service.getClassName())) {
return true;
}
}
return false;
}
Use this in the any class you want to check for a existing service... and if no service is present then just create one or else you are good.
Let me know how it works out.

Categories

Resources