0

我正在开发长期运行的应用程序,我的应用程序定期使用DefaultHttpClient(我也尝试过)。AndroidHttpClient我不知道,但工作一段时间后,不知何故httpClient.execute睡着了。从字面上看,当我唤醒(解锁或连接到 PC)时,我的设备httpClient会立即继续工作(如果我设置超时或仅返回执行响应,则会引发异常)。我搜索并尝试了类似的案例和非工作。对于唤醒和 wifilocks,我只是在执行前获取 wifi 锁和唤醒锁,并在得到响应后释放它。没有任何工作,我不知道。

我工作的设备是 Galaxy S3(v4.0+) 和 Ace(v2.3.6)

提前致谢。

这是卡住的 MyTask,它卡在线路上response = new MyClient(ctx).client.execute(post);并且MyClient是一个包装器,用于处理 https 和证书的事情。

public abstract class GenericTask extends AsyncTask<Object, Object, Document> {


public final static String TAG = "GenericTask";
private WakeLock wakeLock;
private WifiLock wifiLock;
protected HttpPost post;
public String taskId;
public Context ctx;
Object parameter;

public GenericTask(Context ctx, Object parameter) {
    this.parameter = parameter;
    this.ctx = ctx;
    post = null;
    initLock(ctx);
    lock();
}
private void lock() {
    try {
        wakeLock.acquire();
        wifiLock.acquire();
        Log.d(TAG,"LockAcquired");
    } catch (Exception e) {
        Log.e("WlanSilencer", "Error getting Lock: " + e.getMessage());
    }
}

private void unlock() {
    Log.e(TAG,"unlock");
    if (wakeLock.isHeld())
        wakeLock.release();
    if (wifiLock.isHeld())
        wifiLock.release();
    Log.d(TAG,"LockReleased");
}

private void initLock(Context context) {
    wifiLock = ((WifiManager) context
            .getSystemService(Context.WIFI_SERVICE)).createWifiLock(
            WifiManager.WIFI_MODE_FULL_HIGH_PERF, "GenericTaskWifiLock");
    wakeLock = ((PowerManager) context
            .getSystemService(Context.POWER_SERVICE)).newWakeLock(
            PowerManager.FULL_WAKE_LOCK, "GenericTaskWakeLock");
    Log.d(TAG,"LockInitiated");
}

public boolean cancelTask(boolean bln) {
    if(post!=null)
        post.abort();
    return super.cancel(bln);
}

public void execute(){
    if(Build.VERSION.SDK_INT >= 11){
        startNew();
    }else{
        startOld();
    }
}
private void startOld() {
    super.execute();

}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void startNew(){
    executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR );
}
@Override
protected void onPostExecute(Document doc) {
    unlock();
    if(doc==null){
        Log.e("GenericTask: " +getClass(),"onPostExecute doc=null");
    }
    Log.e(TAG,"onPostExecute");
        TaskManager.getInstance().returnFromTaskSuccess(doc, getClass(), taskId, parameter);

}

@Override
protected Document doInBackground(Object... params) {
    Log.e(TAG,"doInBackground");
    try {
        return call();
    } catch (InvalidKeyException e) {
        OtherUtils.printStackTrace(e);
    } catch (NoSuchPaddingException e) {
        OtherUtils.printStackTrace(e);
    } catch (InvalidAlgorithmParameterException e) {
        OtherUtils.printStackTrace(e);
    } catch (IllegalStateException e) {
        OtherUtils.printStackTrace(e);
    } catch (NoSuchAlgorithmException e) {
        OtherUtils.printStackTrace(e);
    } catch (IOException e) {
        OtherUtils.printStackTrace(e);
    } catch (TransformerException e) {
        OtherUtils.printStackTrace(e);
    } catch (ParserConfigurationException e) {
        OtherUtils.printStackTrace(e);
    }
    return null;
}
protected abstract String getUrl();

protected abstract Document getRequest();

public int initialSleep() {
    return 0;
}

public String getTaskId() {
    return taskId;
}

public void setTaskId(String taskId) {
    this.taskId = taskId;
}

protected Document call() throws IOException, TransformerException, ParserConfigurationException, InvalidKeyException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalStateException, NoSuchAlgorithmException{


Document doc = getRequest();
    String url = getUrl();
    Document responseXml = null;
    try {

        post = HttpUtils.postXml(doc, url, null);

        HttpResponse response;
        HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, 16000);
        HttpConnectionParams.setSoTimeout(httpParams, 20000);
        post.setParams(httpParams);
        response = new MyClient(ctx).client.execute(post);
        HttpEntity resEntity = response.getEntity();
        responseXml = XmlUtils.entityToXml(resEntity, null);
        resEntity.consumeContent();
        return responseXml;
    } catch (TransformerException e) {
        throw e;
    } catch (IOException e) {
        throw e;
} 


}
}

HttpUtils:

public class HttpUtils {
public static HttpPost postXml(Document doc, String url, WrapperAsByteArrayOutputStream baos) throws IOException, TransformerConfigurationException, TransformerException
{
    StringWriter xmlAsWriter = new StringWriter();
    StreamResult result = new StreamResult(xmlAsWriter);
    TransformerFactory.newInstance().newTransformer().transform(new DOMSource(doc), result);
    HttpPost post = new HttpPost(url);
    InputStreamEntity req;
    if ( baos == null ) {
        req = new InputStreamEntity(new ByteArrayInputStream(xmlAsWriter.toString().getBytes()), -1);
    }
    else {
        req = new InputStreamEntity(new WrappedInputStream(new ByteArrayInputStream(xmlAsWriter.toString().getBytes()), baos), -1);
    }
    req.setChunked(true);
    post.setEntity(req);
    return post;
}

我的客户:

公共类 MyClient 扩展 DefaultHttpClient { 公共 HttpClient 客户端;

public MyClient(Context ccc) {
    super();
    KeyStore localTrustStore = null;
    try {
        localTrustStore = KeyStore.getInstance("BKS");
    } catch (KeyStoreException e) {
        e.printStackTrace();
    }
    InputStream in = ccc.getResources().openRawResource(
            R.raw.localcert);
    try {
        localTrustStore.load(in, "local_trust_password".toCharArray());
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (CertificateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory
            .getSocketFactory(), 80));
    SSLSocketFactory sslSocketFactory = null;
    try {
        sslSocketFactory = new SSLSocketFactory(localTrustStore);
    } catch (KeyManagementException e) {
        e.printStackTrace();
    } catch (UnrecoverableKeyException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (KeyStoreException e) {
        e.printStackTrace();
    }
    schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));
    HttpParams params = new BasicHttpParams();
    ClientConnectionManager cm = new ThreadSafeClientConnManager(params,
            schemeRegistry);

    this.client = new DefaultHttpClient(cm, params);
}

AlarmReciever 的 onrecieve

public void onReceive(Context context, Intent intent) {
    try {
        TaskManager.getInstance().sendBatteryStatus();
         }
    } catch (Exception e) {
        Toast.makeText(
                context,
                "There was an error somewhere, but we still received an alarm",
                Toast.LENGTH_SHORT).show();
        e.printStackTrace();

    }

returnfromtask success 只是检查 id 的线程安全,处理 doc 并设置新的警报,如下所示:

 Calendar cal = Calendar.getInstance();
    cal.add(Calendar.SECOND, 30);
    Intent intent = new Intent(ctx, AlarmReceiver.class);

    PendingIntent sender = PendingIntent.getBroadcast(ctx, 2131225, intent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    AlarmManager am = (AlarmManager) ctx
            .getSystemService(Context.ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);

sendBatteryStatus 收集一些要发送的信息,并将其传递给扩展 GenericTask 的任务,它通常会执行new MyAsyncTask(..bla bla).execute();

4

1 回答 1

0

一旦您释放唤醒锁,CPU 就可以进入睡眠状态,这很可能就是发生的情况。HttpClient您多次执行一个操作,但在某个时间点,在释放唤醒锁后,CPU 进入休眠状态。

由于您想定期执行代码,您应该使用AlarmManager此处记录的服务:http: //developer.android.com/reference/android/app/AlarmManager.html。即使手机处于睡眠状态,警报管理器服务也处于活动状态。PendingIntent您可以通过其中一种AlarmManager.set*()方法调度来使用它。警报管理器将在接收器方法的持续时间内保持唤醒锁,onReceive()但您很可能需要获取 WiFi 锁。

于 2013-02-08T20:04:12.810 回答