I have an app where I want to build 2 different flow\'s in:
1.b App show\'s an alertbox where user can choose to go to
A solution I just found does require a GET_TASK permission:
ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> services = activityManager
.getRunningTasks(Integer.MAX_VALUE);
boolean isActivityFound = false;
if (services.get(0).topActivity.getPackageName().toString()
.equalsIgnoreCase(getApplicationContext().getPackageName().toString())) {
isActivityFound = true;
}
Log.d("GCM", "Activity open: "+isActivityFound);
UPDATE
In order to launch an alertbox (which isn't possible here) I created a custom alert box and let it use the AlertBox as theme. This is the activity in AndroidManifest.xml:
<activity
android:name="nl.raakict.android.stadseboeren.NotificationAlertActivity"
android:label="@string/title_activity_notification_alert"
android:launchMode="singleInstance"
android:theme="@android:style/Theme.Dialog" >
And the custom activity:
public class NotificationAlertActivity extends Activity implements
Observer, OnClickListener {
private String pendingObjectId;
private ModelFlag modelFlag;
private Database db;
private ArrayList<Discussion> discussions;
private ArrayList<Tip> tips;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification_alert);
findViewById(R.id.button_yes).setOnClickListener(this);
findViewById(R.id.button_no).setOnClickListener(this);
}
I updated the onHandleIntent with this bit:
if (isActivityFound) {
Intent dialogIntent = new Intent(getBaseContext(),
NotificationAlertActivity.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(dialogIntent);
}
Make a class extends Application and implement ActivityLifecycleCallbacks and according to on pause and onResume update a public boolean.
At the time push received check this Boolean and perform as your requirement.
Hope this will help you
public class TestApplication extends Application implements ActivityLifecycleCallbacks{
boolean applicationOnPause = false;
@Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(this);
}
@Override
public void onActivityCreated(Activity arg0, Bundle arg1) {
Log.e("","onActivityCreated");
}
@Override
public void onActivityDestroyed(Activity activity) {
Log.e("","onActivityDestroyed ");
}
@Override
public void onActivityPaused(Activity activity) {
applicationOnPause = true;
Log.e("","onActivityPaused "+activity.getClass());
}
@Override
public void onActivityResumed(Activity activity) {
applicationOnPause = false;
Log.e("","onActivityResumed "+activity.getClass());
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
Log.e("","onActivitySaveInstanceState");
}
@Override
public void onActivityStarted(Activity activity) {
Log.e("","onActivityStarted");
}
@Override
public void onActivityStopped(Activity activity) {
Log.e("","onActivityStopped");
}
}
I'd just write another BroadcastReceiver
like this:
public class MainActivityBroadcastReceiver extends BroadcastReceiver {
public MainActivityBroadcastReceiver(Activity mainActivity) {
}
@Override
public void onReceive(Context context, Intent intent) {
Toast toast = Toast.makeText(context, "hola", Toast.LENGTH_LONG);
toast.show();
abortBroadcast();
}
}
and then in the MainActivity
I'd override the methods and then I don't receive the notification so I intercept the `Intent~ and abort the other broadcast:
@Override
protected void onPause() {
unregisterReceiver(mainActivityBroadcastReceiver);
super.onPause();
}
@Override
public void onResume() {
IntentFilter filter = new IntentFilter("com.google.android.c2dm.intent.RECEIVE");
filter.setPriority(1);
registerReceiver(mainActivityBroadcastReceiver, filter);
super.onResume();
}
In my code I have public static ArrayList<Activity> activity_stack
every activity is added in oncreate and removed in ondestroy methods. I check this stack in GcmBroadcastReceiver. I don't know if it's a good solution.