Reputation: 66555
I created an application which enables the user to set whether he wants to receive notification while the application runs in background mode. If the notifications are enabled an activity should be started (the dialog should appear on the screen).
I tried to enabled it the following way:
@Override
public void onProductsResponse(List<Product> products) {
this.products = products;
moboolo.setProducts(products);
if(moboolo.getAutomaticNotificationsMode() != 0 && products.size() > 0){
if(isRunningInBackground)
{
Intent intent = new Intent(this, ProductListActivity.class);
intent.setAction(Intent.ACTION_MAIN);
startActivity(intent);
}
}
drawProducts(products);
}
this is the method from main activity. When onPause() is executed isRunningInBackground is set true. When I tried to debug it when the main application was running in the background the line
startActivity(intent) had no effect (the activity didn't appear).
Does anyone know how to midify the logic in order to start an activity from the main activity when the main activity is running in the background (after onPause() is called)?
Thank you.
Upvotes: 3
Views: 12288
Reputation: 6016
To complete Hemendra's answer, you don't need any of those flags except FLAG_ACTIVITY_REORDER_TO_FRONT
. You just need to create a PendingIntent from your normal intent and call the new PendingIntent's send() method to dispatch the intent. Here is how I did it:
Intent yourIntent = new Intent(this, YourActivity.class);
// You can send extra info (as a bundle/serializable) to your activity as you do
// with a normal intent. This is not necessary of course.
yourIntent.putExtra("ExtraInfo", extraInfo);
// The following flag is necessary, otherwise at least on some devices (verified on Samsung
// Galaxy S3) your activity starts, but it starts in the background i.e. the user
// doesn't see the UI
yourIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0,
yourIntent, 0);
try {
pendingIntent.send(getApplicationContext(), 0, yourIntent);
} catch (Exception e) {
Log.e(TAG, Arrays.toString(e.getStackTrace()));
}
Upvotes: 5
Reputation: 1060
Intent i= new Intent("android.intent.category.LAUNCHER");
i.setClass(getApplicationContext(), MyActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
PendingIntent i2 = PendingIntent.getActivity(getApplicationContext(), 0, insIntent,Intent.FLAG_ACTIVITY_NEW_TASK);
try {
i2.send(getApplicationContext(), 0, i);
} catch (Exception e) {
e.printStackTrace();
}
And on MyActivity's onCreate...
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
This will bring your activity to front event if main activity is running in background.
Upvotes: 2
Reputation: 193696
You can't force an Activity
to appear from an application running the background. The documentation says:
If the application is running in the background and needs the user's attention, the application should create a notificaiton that allows the user to respond at his or her convenience.
If your Activity
is paused the user may be doing something else in a different application and probably doesn't want your Activity
suddenly appearing on top of what they are currently doing.
You should be using a Status Bar Notification. This allows your application to put an icon in the Status Bar. The user can then slide down the Status Bar drawer and click on your notification to open your application and show the relevant Activity
. This is the way the vast majority of Android apps notify the user when running in the background.
Upvotes: 7