Notification on Onfinish() function of a CountDownTimer class - android

I am trying to build a Count down timer app. where i need to notify user when Coundown ends to 0. i use CountDownTimer class to Count.
Here is what i wanted:
when the countdown hit 0 a notification will appear on status bar with notification tune with vibration(lets say 200ms) and when they press notification it goes the app page where they left last time(I dont want open any Activity). also if user still on the app and notification comes on status bar, When they press it,it goes right where they are(Dont want start any activity).
Here is what i already tried:
i tried notification on onFinish() function of CountDownTimer class. it work just fine but when user press the notification it just start new Activity instead of the current app page.
Its been 2 days i search for the solution still no working solution.
so Any help would be appreciated! Thanks!

Check if the application is running in background or not and either push the new activity (in background scenario) or leave it as it is (as user is already in app)
boolean isInBackground = true;
ActivityManager am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
ActivityManager.RunningTaskInfo foregroundTaskInfo = am.getRunningTasks(1).get(0);
String foregroundTaskPackageName = foregroundTaskInfo .topActivity.getPackageName();
PackageManager pm = context.getPackageManager();
PackageInfo foregroundAppPackageInfo = pm.getPackageInfo(foregroundTaskPackageName, 0);
String foregroundTaskAppName = foregroundAppPackageInfo.applicationInfo.loadLabel(pm).toString();
if(!"Your App name".equalsIgnoreCase(foregroundTaskAppName) ){
isInBackground=false;
}
App name should be the one in strings.xml
<string name="app_name">Your app name</string>

Related

How to get package name of any app when it gets opened by user?

I need to get package name or app name of any app when it gets opened by user. I searched about this but didn't get proper example.
Anyone can give me suggestions for this case?
Thank you so much.!
You have to create your background service for that , which will continuously monitor top app in device .
So first of all make a service which will start after your app launch .In your service , use this
ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager.getRunningTasks(1);
ActivityManager.RunningTaskInfo ar = RunningTask.get(0);
activityOnTop=ar.topActivity.getClassName();
activityOnTop will give you the current running app in your phone . Now do whatever you want after getting top activity.

Check if Activity is Open Before Displaying Push Notifications

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

Android service that detects if any app is opened by the user and starts timer in the background

App which starts a service which keeps running in background till the user wishes to terminate. How to detect if any other app is opened or closed? I want to start a unique timer for different apps. And finally store all the logs. Any help would be appreciated.
Thanks..
Simply get current running task using ActivityManager
ActivityManager mActivityManager= (ActivityManager) mContext
.getSystemService(Activity.ACTIVITY_SERVICE);
if(Build.VERSION.SDK_INT > 20)
{
String mPackageName = mActivityManager.getRunningAppProcesses().get(0).processName;
}
else
{
String mpackageName = mActivityManager.getRunningTasks(1).get(0).topActivity.getPackageName();
}
And you can also detect an app processs change's using this
Also See this Example

Close the service from the library only when the entire app goes to the background

I searched a lot in the net about it, the solutions are based on onPause and onDestroy. I want to give a library to a developer who just needs to paste a few lines of code from the library in his app which will enable the developer to create a service and destroy it when his entire app is in the background.
Does the Android OS send some kind of signals or intents when the app or the activity is changed (other than onPause or onStop method), so that i can catch that in a broadcast receiver from my library and do some actions.
If you know the package name of your app, try this (put this following snippet in the onCreate method of your app):
ActivityManager am= (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
Then,
boolean exit = false;
while(!exit)
{
List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
if(!componentInfo.getPackageName().equals("Your package name"))
{
//Do your work here
exit = true;
}
}
When you start your app, this will be put into componentInfo. The taskInfo.get(0).topActivity will return the activity in the foreground. Hence you can know that your app has gone to the background by comparing package using the second code snippet.
Note:Put this second code snippet in an Asynctask so that the checking of whether the app has started can be done in the background.

Receiver for Android App info activity

Is there any way to create a BroadcastReceiver that can be fired when user goes to App info activity (Settings > Application Management > Pick an app)? If so, will we know which app's info is showing up?
Thanks,
hmlaken
There is no broadcast which is fired when user opens a particular activity.
If you want to know the activity which the user is currently on, you can try to periodically check the activity stack to know which activity the user is currently viewing.
while(true) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> runTaskInfo = am.getRunningTasks(1);
String pkg = runTaskInfo.get(0).topActivity.getPackageName();
String activity = runTaskInfo.get(0).topActivity.getClassName();
String tmp = pkg + " : " + activity;
Thread.sleep(1000);
}
Run this in a thread and it will wake up every 1 second and give you the activity which is currently on the screen. Compare it with activity you are looking for to know when the user calls it.
No, there is no Intent broadcasted.
(Also why do you want this information?)
If you want to know when an user removes your app: listen to the PACKAGE_REMOVED intent.

Categories

Resources