0

我有一些在屏幕打开时执行的代码,但是当我关闭屏幕电源时,执行中有一些问题(即使在全局获取 Wakelock 之后)。我有一个获取唤醒锁并调用活动的服务 - 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!!"); 
 }      

在我的 ScriptActivity(调用 Service 并执行需要 30 秒的操作)中。但是,在我的情况下没有调用 onActivityResult() 。我不确定为什么会发生这种情况。请帮我。

我在 handler.postDelayed() 块中定义了 setResult()。我有如下代码:

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);

更新:使用绑定服务也不起作用。对此有什么想法吗?

4

1 回答 1

0

当我的活动有焦点并且我关闭屏幕时,它会停止。根据开发者页面,停止:在这种状态下,活动完全隐藏,对用户不可见;它被认为是在后台。停止时,活动实例及其所有状态信息(如成员变量)被保留,但不能执行任何代码。

此外,我修改了代码,以便我的服务可以直接调用 ScriptService 类,而无需任何活动。在我的服务的 onCreate() 方法中,我使用在不同活动中将唤醒锁释放到获取位置的链接来获取全局唤醒锁。

于 2013-02-26T14:04:31.030 回答