データベース暗号化のパスワードを処理するために複数のアクティビティを持つアプリで 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)
この問題を解決する方法はありますか?