Broadcast Receiver to Detect Application Start

How can one detect an Android application launching?

The application start Intent is not a broadcast, so there is no way to register a broadcast receiver and receive it. As previously answered here, there really is no way to detect the launch of the app. You could possibly write a service that polled the running tasks looking for the application's task (using the ActivityManager interface), but that's the best I can think of and it probably wouldn't be very performant.

how to detect app launching event in android and start broadcast reciever

There is no broadcast to know when an app is launched. You can have a service running which has to periodically check the currently running tasks list to see if a particular app has been launched.

You definitely can make the service run with no notification icon, but that will simply increase the probability of your service getting killed.

You can learn about services from here : http://developer.android.com/reference/android/app/Service.html

You can get the list of running tasks from getRunningTasks() method of the ActivityManager.

Best way to detect (any) app (i.e. activity) launch from a my background service

Perhaps my answer in this question might help you. Android how to know an app has been started and range apps priority according the starting times

Place that code inside your background service.

Based on the package name of the activity in the foreground, you can detect that app name by checking all the apps on the phone and matching it with the app that has the same package name.

Edit

Since you want a continuous check, you could use an AsyncTask(), and in the doInBackground() function, just keep getting the list of running activities in each iteration, like I have suggested in that link, and the first item in the taskInfo list will have the activity/app in the foreground. You can also provide another button in your app which on click you stop this , by keeping a bool variable in shared preferences (which you poll for in the while condition in doInBackGround() function of the AsyncTask()).

I have done something similar in the past, and it worked for me.

Detect if the application is manually closed or not

Your app will still receive events, even if it isn't running. Before you do anything in onReceive(), you can check if the activity is running by:

Option 1: Use a static variable in your activity to check it's state for access from the receiver :

public class YourActivity extends Activity {

public static boolean isRunning = false;

@Overrride
public void onCreate(Bundle savedInstanceState){
isRunning = true;
....
}
//We need receiver to work when app is minimized
/*
@Override
public void onStart() {
super.onStart();
isRunning = true;
}

@Override
public void onStop() {
super.onStop();
isRunning = false;
}
*/

}

And in the receiver:

public class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
Log.d("mylog", "NetworkChangeReceiver Hit");
if(!YourActivity.isRunning)
return;
}
}

Option 2 : Using the ActivityManager

public class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {

if (isAppForground(context))
return;

}

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

}

You'll need the permission:

<uses-permission android:name="android.permission.GET_TASKS" />

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



Leave a reply



Submit