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