0

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?

user1741274
  • 759
  • 3
  • 13
  • 25
  • ExecuteSript is an activity right? in your handler postDelayed code instead of Intent data = new Intent(ScriptActivity.this,ExecuteScript.class); try Intent data= new Intent(); why add the delay btw ?? – baboo Feb 06 '13 at 09:48
  • Ya ExecuteScript is an Activity. I initially had Intent data = new Intent(), but it did not work. So I added,Intent data = new Intent(ScriptActivity.this,ExecuteScript.class); The delay is because the ScriptService executes a script that takes maximum of 30 seconds and displays toast in the phone. Hence I have setResult in handler. – user1741274 Feb 06 '13 at 10:53
  • I used the link http://stackoverflow.com/questions/5302085/onactivityresult-never-called and added it to my code. But it didnt make any difference. @baboo – user1741274 Feb 06 '13 at 10:55
  • instead of 30 sec delay use binding service , on successful binding it returns via callback there setresult and finish activity .. look into binding service (instead of startservice use bindservice) – baboo Feb 06 '13 at 11:15
  • I am now using bindService() and removed the handler. In this case, I am getting execption like: Activity ScriptActivity has leaked ServiceConnection that was originally bound here.. But even now, the onActivityResult() is not called. Am I doing it right because I dont have much idea using binded services. @baboo – user1741274 Feb 06 '13 at 12:30
  • follow this http://www.ozdroid.com/#!BLOG/2010/12/19/How_to_make_a_local_Service_and_bind_to_it_in_Android – baboo Feb 06 '13 at 13:08
  • dont forget to unbindservice – baboo Feb 06 '13 at 13:12
  • @baboo Thanks for the links. But as I have removed the handler, setResult() in if block is executed and I unbind the connection in ScriptActivity. Then my actual script gets executed and the code is not returned to ExecuteScript. I will update the code changes made soon. – user1741274 Feb 06 '13 at 14:15
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/24039/discussion-between-baboo-and-user1741274) – baboo Feb 06 '13 at 14:47

1 Answers1

0

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.

Community
  • 1
  • 1
user1741274
  • 759
  • 3
  • 13
  • 25