使用CacheWord的活动泄露了ServiceConnection

时间:2013-10-09 21:36:01

标签: service

尝试在具有多个活动的应用程序中使用CacheWord来处理我的数据库加密密码我随机得到活动泄漏ServiceConnection的错误...最初绑定在此处。

我认为这个问题是由于在调用onDestroy()时没有彻底断开服务的连接。但我不知道该怎么做。

我的MainActivity的结构:

package de.my.app;

import info.guardianproject.cacheword.CacheWordActivityHandler;
import info.guardianproject.cacheword.CacheWordHandler;
import info.guardianproject.cacheword.ICacheWordSubscriber;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
import java.text.SimpleDateFormat;
import java.util.Date;

import net.sqlcipher.database.SQLiteDatabase;

import android.net.ParseException;
import android.os.Bundle;
import android.os.Environment; 
import android.app.ActionBar; 
import android.app.Activity;
import android.app.ActionBar.OnNavigationListener;
import android.content.Intent;
import android.database.Cursor;
import android.app.Fragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Toast;

public class Home extends Activity implements ICacheWordSubscriber {

private static final String TAG = Home.class.getSimpleName();
private Menu mainMenu;

// removed more definitions

private CacheWordActivityHandler mCacheWord;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    SQLiteDatabase.loadLibs(this);
    mCacheWord = new CacheWordActivityHandler(this, this); 

}

@Override
protected void onPause() {
    super.onPause();
    mCacheWord.onPause();

    Log.d(TAG,"onPause()");
}

@Override
protected void onResume() { 
    super.onStart(); 
    mCacheWord.onResume();

    Log.d(TAG,"onResume()");
}

private void closeDatabase()
    {
        Log.d(TAG,"closeDatabase()");

        if (dbHandler != null) {
            dbHandler.close();
            dbHandler = null;
        }
    }

void showLockScreen() {

    Log.d(TAG,"showLockScreen()");

    Intent intent = new Intent(this, LockScreenActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    intent.putExtra("originalIntent", getIntent());
    startActivity(intent);
    finish();
}

void lock() {

    Log.d(TAG,"lock()");

    closeDatabase();
    System.gc();
    showLockScreen();
}

private void unlockDatabase()
{

    Log.d(TAG,"unlockDatabase()");

    dbHandler = new TBOpenHandler(mCacheWord, this);

    try
    {

        dbHandler.open();

    } catch (Exception e)
    {
        e.printStackTrace();
        Toast.makeText(this, getString(R.string.err_pass), Toast.LENGTH_LONG).show();
    }
}

@Override
public void onCacheWordUninitialized() {
    Log.d(TAG, "onCacheWordUninitialized()");
    showLockScreen();
}

@Override
public void onCacheWordLocked() {
    Log.d(TAG, "onCacheWordLocked()");
    lock();
}

@Override
public void onCacheWordOpened() {
    Log.d(TAG, "onCacheWordOpened()");
    unlockDatabase();

    if (dbHandler.isOpen()) { 
        inhalt();
    }
}

private void inhalt() {

    // removed the content

}

}

还有错误消息:

10-09 22:57:38.009: E/ActivityThread(16122): Activity de.my.app.Home has leaked ServiceConnection info.guardianproject.cacheword.CacheWordHandler$2@41fe8738 that was originally bound here
10-09 22:57:38.009: E/ActivityThread(16122): android.app.ServiceConnectionLeaked: Activity de.my.app.Home has leaked ServiceConnection info.guardianproject.cacheword.CacheWordHandler$2@41fe8738 that was originally bound here
10-09 22:57:38.009: E/ActivityThread(16122):    at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:974)
10-09 22:57:38.009: E/ActivityThread(16122):    at  android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:868)
10-09 22:57:38.009: E/ActivityThread(16122):    at android.app.ContextImpl.bindServiceAsUser(ContextImpl.java:1452)
10-09 22:57:38.009: E/ActivityThread(16122):    at android.app.ContextImpl.bindService(ContextImpl.java:1440)
10-09 22:57:38.009: E/ActivityThread(16122):    at android.content.ContextWrapper.bindService(ContextWrapper.java:496)
10-09 22:57:38.009: E/ActivityThread(16122):    at info.guardianproject.cacheword.CacheWordHandler.connectToService(CacheWordHandler.java:74)
10-09 22:57:38.009: E/ActivityThread(16122):    at info.guardianproject.cacheword.CacheWordActivityHandler.onResume(CacheWordActivityHandler.java:15)
10-09 22:57:38.009: E/ActivityThread(16122):    at de.my.app.Home.onResume(Home.java:457)
10-09 22:57:38.009: E/ActivityThread(16122):    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
10-09 22:57:38.009: E/ActivityThread(16122):    at android.app.Activity.performResume(Activity.java:5211)
10-09 22:57:38.009: E/ActivityThread(16122):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2780)
10-09 22:57:38.009: E/ActivityThread(16122):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2819)
10-09 22:57:38.009: E/ActivityThread(16122):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2266)
10-09 22:57:38.009: E/ActivityThread(16122):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-09 22:57:38.009: E/ActivityThread(16122):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-09 22:57:38.009: E/ActivityThread(16122):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 22:57:38.009: E/ActivityThread(16122):    at android.os.Looper.loop(Looper.java:137)
10-09 22:57:38.009: E/ActivityThread(16122):    at android.app.ActivityThread.main(ActivityThread.java:5103)
10-09 22:57:38.009: E/ActivityThread(16122):    at java.lang.reflect.Method.invokeNative(Native Method)
10-09 22:57:38.009: E/ActivityThread(16122):    at java.lang.reflect.Method.invoke(Method.java:525)
10-09 22:57:38.009: E/ActivityThread(16122):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-09 22:57:38.009: E/ActivityThread(16122):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-09 22:57:38.009: E/ActivityThread(16122):    at dalvik.system.NativeStart.main(Native Method)

有没有办法解决这个问题?

1 个答案:

答案 0 :(得分:0)

答案有点迟,但你永远不知道。

我遇到了同样的问题,因为我在AndroidManifest中没有提供正确的服务。

我的意思是,我有服务,但是,当我做了名称包的重构时,eclipse改变了服务的包。

所以请看看AndroidManifest,并确保你有这样的方式:

<service
     android:name="info.guardianproject.cacheword.CacheWordService"
     android:enabled="true"
     android:exported="false" />