0

On My Activity , i start a service to register a broadcast receiver for receiving event screen off. I want when screen off , my activity will auto start.

Code on onReceive() of receiver :

public void onReceive(Context ct, Intent intent) {
        if (Intent.ACTION_SCREEN_OFF.equalsIgnoreCase(intent.getAction())) {

            final Context context;
            context = ct;




            Intent i = new Intent(context, ScreenSaver.class);
            //i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
            context.startActivity(i);

        } 

    }

Code on Oncreate() of Activity

protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.screensaver);
         mainLayout = (LinearLayout)findViewById(R.id.mainLayout);
        mainLayout.setOnClickListener(this);




        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                |WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                |WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                |WindowManager.LayoutParams.FLAG_FULLSCREEN);


    }

I installed app on my phone, when screen auto off or press Power button, my activity auto start OK (phone is connecting by cable with PC)

But when I remove cable, when screen auto off, my activity did not start. I do not know why have different. Anybody can help and explain clearly for me ? Thanks

CauCuKien
  • 351
  • 1
  • 2
  • 12
  • 1
    I guess the device stops the CPU on standby to save energy but this is only necessary while not charging battery. A partial wakelock may help. – Michael Butscher Jun 02 '13 at 15:42
  • 1
    ScreenSaver? You really should not be running anything like that when the device is running on battery... Also, take a look on [DreamService](https://developer.android.com/reference/android/service/dreams/DreamService.html) – ozbek Jun 02 '13 at 15:50
  • @MichaelButscher can you explain detailly please ? How to prevent device stop CPU on standby to save energy ? – CauCuKien Jun 02 '13 at 17:24
  • @BinhVova Please refer to http://developer.android.com/reference/android/os/PowerManager.html – Michael Butscher Jun 02 '13 at 20:14

0 Answers0