I have created a BroadCastReceiver for SMS and I want it to work only when the application is running (either in forgeground or background) . The current code im using is :
public class BroadCastReceiver extends BroadcastReceiver
{
public void onReceive(Context context, Intent intent)
{
ActivityManager am = (ActivityManager) context
.getSystemService(Activity.ACTIVITY_SERVICE);
String packageName = am.getRunningTasks(1).get(0).topActivity
.getPackageName();
if(packageName.contains("com.example.sms") )
{
abortBroadcast();
}else
{
}
}
}
But the problem is this code only works in foreground , so when the user hits back button or home button the application will work in background but the BroadCastReceiver will not work . So is there's any method to solve this issue ?? &Thanks in advance