SHIDHIN TS
SHIDHIN TS

Reputation: 1665

How to open application when screen is off

I want to open my application when I got a push notification. Now the application is not opening when the push received. Here is the code I am using,

Added WAKE_LOCK Permission on manifest like,

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

Here is my code to open the application,

 Intent intent = new Intent(this, HomeActivity.class);
 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 intent.putExtra(Constants.NewOrderRequest, true);
 startActivity(intent);

Upvotes: 0

Views: 69

Answers (2)

sourabh kaushik
sourabh kaushik

Reputation: 523

use this to fire the activity from the service

Intent inte = new Intent(context, Activity.class);


                    inte.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    inte.addCategory(intent.CATEGORY_LAUNCHER);
                    context.startActivity(inte);

and in the activity place this in onCreate

   Window window=this.getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

Upvotes: 1

Seraph Gabriel
Seraph Gabriel

Reputation: 51

May this can help you.But you must do it in a Services.

private ScreenObserver screenObserver;
public void onCreate() {
    IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    filter.addAction(Intent.ACTION_SCREEN_ON);
    screenObserver = new ScreenObserver();

    registerReceiver(screenObserver, filter);
}
private class ScreenObserver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                if (isDebug) Log.d(TAG, "ScreenON");
                Intent intent = new Intent(this, HomeActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra(Constants.NewOrderRequest, true);
                startActivity(intent);
            }
            if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                if (isDebug) Log.d(TAG, "ScreenOFF");
            }
        }
    }

Upvotes: 0

Related Questions