I have some code that gets executed when the screen is on, but when I power off the screen, has some problems in execution(even after acquiring Wakelock globally). I have a service that acquired a wakelock and calls the activity - ExecuteScript with the following code:
ExecuteScript.java:
Intent intent = new Intent(this,ScriptActivity.class);
intent.putExtra("code",code);
intent.putExtra("type", type);
startActivityForResult(intent,REQUEST_CODE);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d(TAG,"The result code value inside onActivityResult() is: "+requestCode +resultCode);
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
Log.d(TAG,"ScriptActivity finished success!!");
}
And in my ScriptActivity(that calls Service and performs an operation that takes 30 seconds). However, the onActivityResult() is not called in my case. I am not sure why this occurs. Please help me.
I have defined the setResult() inside handler.postDelayed() block. I have code like:
ScriptActivity.java
Intent serviceIntent = new Intent(this,ScriptService.class);
serviceIntent.putExtra("code",code);
serviceIntent.putExtra("type",type);
startService(serviceIntent);
handler.postDelayed(new Runnable(){public void run(){
Intent data = new Intent(ScriptActivity.this,ExecuteScript.class);
data.putExtra("returnKey1", "You can write the file to the server ");
setResult(Activity.RESULT_OK, data);
Log.d(TAG,"**After calling the method startActivityTwo here*****");
finish();
}},30000);
Update: Using binded services also didnt work. Any ideas on this?
When my activity has focus, and I turn off the screen, it gets stopped. According to the developer pages, Stop: In this state, the activity is completely hidden and not visible to the user; it is considered to be in the background. While stopped, the activity instance and all its state information such as member variables is retained, but it cannot execute any code.
Also, I modified the code so that my service can directly call the ScriptService class without any activities in between. In my service's onCreate() method I acquire the global wakelock using the link releasing wakelock in different activity to where it was acquired.