How to get started when the main action is running in the background?

I created an application that allows the user to set whether he wants to receive a notification when the application is running in the background. If notifications are activated, an action must be started (a dialog should appear on the screen).

I tried to enable it as follows:

@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 a method from the main activity. When onPause () is executed, isRunningInBackground is set to true. When I tried to debug it when the main application was running in the background, the line

startActivity (intent) did not affect (activity did not appear).

Does anyone know how to blur logic in order to start activity from the main action when the main action is performed in the background (after calling onPause ()?

Thank.

+3
3

Activity , . :

, , .

Activity , - , , , Activity , .

. . , Activity. Android .

+7

Hemendra, , FLAG_ACTIVITY_REORDER_TO_FRONT. PendingIntent send() PendingIntent . :

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

MyActivity 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);

, .

+1

Source: https://habr.com/ru/post/1727937/


All Articles