2014-11-14 57 views
0

我的應用程序依賴於Android獨立類的核心模塊來執行業務邏輯。這些對象需要可用於應用程序的所有部分,並且還需要在整個應用程序中保持其狀態。哪裏可以創建業務邏輯對象並保持狀態?

我一直在應用程序子類中實例化這些對象,並在我的活動和服務中使用它們。但是,當Android決定終止我的應用程序以釋放內存時,這些業務邏輯對象也會被終止並失去它們的狀態。

Android應用程序中有狀態業務對象模型的最佳策略是什麼?

+0

創建一個類並將其序列化並保存到共享首選項或任何其他由android提供的存儲選項。 –

+0

它更好地提取您的關鍵對象的所有屬性,並將它們轉儲到持久存儲中,例如android中的「SharedPreferences」,下一次當您的應用程序重新啓動時,重新從這些值啓動您的對象。 – Techfist

+0

請記住,在'SharedPreferences'中,您只能存儲像'boolean,float,int,long,String'等值的原語。 – Mayank

回答

0

如果要在用戶關閉應用程序時保存它們,則必須將它們保存在共享首選項或數據庫或手機內存中的文件中! 和用於內部應用,可以簡單地在從SerializableParcelable延伸的類定義它們,並將它們傳遞活動/片段之間的/ etc

1

此完美解決方案是使用持久性存儲。我遵循的是有點複雜但非常有用。 它在三個部分被劃分:

  1. SQLite數據庫
  2. API助手
  3. Singleton類(數據助手)

和以下步驟:

  1. 檢索的所有數據db到單個實例類。
  2. 每當我需要更新它,然後在課堂上更新。
  3. 在某些特定事件中將其轉儲到數據庫中。 (如按鈕點擊或後按)。

通過這種方式,你有你的數據的完整和容易獲得通過在應用軟件

  1. 數據庫:不再詳述了。它可以是任何持久性存儲,如數據庫或首選項。

  2. API幫助程序:示例api幫助程序類。取決於您的存儲空間,它可以是任何東西。

    public class AppsApi {  
    private static AppsApi appInstance; 
    private Context mContext; 
    
    public static AppsApi getInstance(Context context) { 
    
        if (appInstance == null) 
         appInstance = new AppsApi(context); 
        return appInstance; 
    
    } 
    
    public AppsApi(Context context) { 
        this.mContext = context; 
    } 
    
    public static String PROJECTION[]; 
    
    static { 
        String[] arrayOfString = new String[5]; 
        arrayOfString[0] = AppsTable.COLUMN_ID; 
        arrayOfString[1] = AppsTable.COLUMN_APP_NAME; 
        arrayOfString[2] = AppsTable.COLUMN_APP_PACKAGE_NAME; 
        arrayOfString[3] = AppsTable.COLUMN_APP_ACTIVITY_NAME; 
        arrayOfString[4] = AppsTable.COLUMN_IS_FROM_DASHBOARD; 
        PROJECTION = arrayOfString; 
    } 
    
    public int insertApp(ContentValues app) { 
    
        Uri uri = mContext.getContentResolver().insert(
          DashDroidContentProvider.CONTENT_URI_APP, app); 
        System.out.println("APP Added URI :: " + uri); 
        return Integer.parseInt(uri.getLastPathSegment()); 
    
    } 
    
    public Cursor getAllApps() { 
        return mContext.getContentResolver().query(
          DashDroidContentProvider.CONTENT_URI_APP, AppsApi.PROJECTION, 
          null, null, null); 
    } 
    
    public Cursor getAllApp(boolean isFromDashBoard) { 
        int is = isFromDashBoard ? 1 : 0; 
        return mContext.getContentResolver().query(
          DashDroidContentProvider.CONTENT_URI_APP, AppsApi.PROJECTION, 
          AppsTable.COLUMN_IS_FROM_DASHBOARD + " LIKE ?", 
          new String[] { is + "" }, null); 
    } 
    
    public void deleteApp(int id) { 
        mContext.getContentResolver().delete(
          DashDroidContentProvider.CONTENT_URI_APP, 
          AppsTable.COLUMN_ID + " = ?", new String[] { id + "" }); 
    } 
    
    public void deleteApp(String packageName) { 
        mContext.getContentResolver().delete(
          DashDroidContentProvider.CONTENT_URI_APP, 
          AppsTable.COLUMN_APP_PACKAGE_NAME + " LIKE ?", 
          new String[] { packageName + "" }); 
    } 
    
    public void updateApp(ContentValues app, int id) { 
        int uri = mContext.getContentResolver().update(
          DashDroidContentProvider.CONTENT_URI_APP, app, "_id=?", 
          new String[] { id + "" }); 
        System.out.println("App Updated URI ::" + uri); 
    } 
    
    public void clear() { 
        mContext.getContentResolver().delete(
          DashDroidContentProvider.CONTENT_URI_APP, null, null); 
    } 
    
    public int count() { 
        return ((Cursor) mContext.getContentResolver().query(
          DashDroidContentProvider.CONTENT_URI_APP, 
          new String[] { AppsTable.COLUMN_ID }, null, null, null)) 
          .getCount(); 
    } 
    
    public Cursor filterApp(String selection, String[] selectionArgs) { 
    
        return mContext.getContentResolver().query(
          DashDroidContentProvider.CONTENT_URI_APP, AppsApi.PROJECTION, 
          selection, selectionArgs, null); 
    } 
    
    public int insertBulkApps(ContentValues[] apps) { 
    
        int noOfRecordInserted = mContext.getContentResolver().bulkInsert(
          DashDroidContentProvider.CONTENT_URI_APP, apps); 
        System.out.println("Inserted Record Count :: " + noOfRecordInserted); 
        return noOfRecordInserted; 
    
    } 
    

    }

  3. 數據助手:它是一個單實例類。通過應用程序爲您提供數據。這是巨大而簡單的。

    public class AppsHelper { 
    
    public static final int TYPE_SAVE = 0; 
    public static final int TYPE_GET = 1; 
    public static final int TYPE_UPDATE = 2; 
    
    private AppsData[] appsDashBoard; 
    private AppsData[] appsMoreApps; 
    
    private AppsApi appsProvider; 
    
    private OnDataBaseUpdateListener mListener; 
    
    private static AppsHelper mHelper; 
    
    public AppsHelper(Context context) { 
        appsProvider = AppsApi.getInstance(context); 
        initData(); 
        if (appsProvider.count() == 0) { 
         new saveDataTask().execute(); 
        } else { 
         updateAppsFromDatabase(); 
        } 
    
    } 
    
    public static AppsHelper getInstance(Context context) { 
    
        if (mHelper == null) 
         mHelper = new AppsHelper(context); 
    
        return mHelper; 
    
    } 
    
    private void initData() { 
    
        appsDashBoard = new AppsData[DashDroidConstants.NO_OF_DASH_BOARD_APPS]; 
        appsMoreApps = new AppsData[DashDroidConstants.NO_OF_MORE_APPS]; 
    
        for (int i = 0; i < appsDashBoard.length; i++) { 
         appsDashBoard[i] = new AppsData(i, "null", "null", "null"); 
        } 
    
        for (int i = appsDashBoard.length; i < (appsMoreApps.length + appsDashBoard.length); i++) { 
         appsMoreApps[i - appsDashBoard.length] = new AppsData(i, "null", 
           "null", "null"); 
        } 
    
    } 
    
    public void updateMoreApp(String appName, String activityName, 
         String appPackageName, int index) { 
        appsMoreApps[index].setData(appName, activityName, appPackageName); 
        new updateDataTask(false, index).execute(); 
    } 
    
    public void updateMoreApp(String appName, String activityName, 
         String appPackageName, int index, OnDataBaseUpdateListener listener) { 
        appsMoreApps[index].setData(appName, activityName, appPackageName); 
        this.mListener = listener; 
        new updateDataTask(false, index).execute(); 
    } 
    
    public void updateDashBoardApp(String appName, String activityName, 
         String appPackageName, int index) { 
        appsDashBoard[index].setData(appName, activityName, appPackageName); 
        new updateDataTask(true, index).execute(); 
    } 
    
    public void updateDashBoardApp(String appName, String activityName, 
         String appPackageName, int index, OnDataBaseUpdateListener listener) { 
        appsDashBoard[index].setData(appName, activityName, appPackageName); 
        this.mListener = listener; 
        new updateDataTask(true, index).execute(); 
    } 
    
    public void updateAppsFromDatabase() { 
        new getDataTask().execute(); 
    } 
    
    public AppsData[] getDashBoardApps() { 
        return appsDashBoard; 
    } 
    
    public AppsData[] getMoreApps() { 
        return appsMoreApps; 
    } 
    
    private void updateAppInDatabase(boolean isDashBoardApp, int index) { 
        ContentValues cv = new ContentValues(); 
        cv = new ContentValues(); 
        if (isDashBoardApp) { 
         cv.put(AppsTable.COLUMN_APP_PACKAGE_NAME, 
           appsDashBoard[index].getPackageName()); 
         cv.put(AppsTable.COLUMN_APP_ACTIVITY_NAME, 
           appsDashBoard[index].getActivityName()); 
         cv.put(AppsTable.COLUMN_APP_NAME, appsDashBoard[index].getAppName()); 
        } else { 
         cv.put(AppsTable.COLUMN_APP_PACKAGE_NAME, 
           appsMoreApps[index].getPackageName()); 
         cv.put(AppsTable.COLUMN_APP_ACTIVITY_NAME, 
           appsMoreApps[index].getActivityName()); 
         cv.put(AppsTable.COLUMN_APP_NAME, appsMoreApps[index].getAppName()); 
        } 
    
        int dbIndex = isDashBoardApp ? index : index + appsDashBoard.length; 
        appsProvider.updateApp(cv, dbIndex); 
    
    } 
    
    private int saveDataInDatabase() { 
        ContentValues[] cv = new ContentValues[appsDashBoard.length 
          + appsMoreApps.length]; 
    
        for (int i = 0; i < appsDashBoard.length; i++) { 
         cv[i] = new ContentValues(); 
         cv[i].put(AppsTable.COLUMN_ID, appsDashBoard[i].getId()); 
         cv[i].put(AppsTable.COLUMN_APP_PACKAGE_NAME, 
           appsDashBoard[i].getPackageName()); 
         cv[i].put(AppsTable.COLUMN_APP_ACTIVITY_NAME, 
           appsDashBoard[i].getActivityName()); 
         cv[i].put(AppsTable.COLUMN_APP_NAME, appsDashBoard[i].getAppName()); 
         cv[i].put(AppsTable.COLUMN_IS_FROM_DASHBOARD, "1"); 
        } 
    
        for (int i = appsDashBoard.length; i < (appsMoreApps.length + appsDashBoard.length); i++) { 
         cv[i] = new ContentValues(); 
         cv[i].put(AppsTable.COLUMN_ID, appsMoreApps[i 
           - appsDashBoard.length].getId()); 
         cv[i].put(AppsTable.COLUMN_APP_PACKAGE_NAME, appsMoreApps[i 
           - appsDashBoard.length].getPackageName()); 
         cv[i].put(AppsTable.COLUMN_APP_ACTIVITY_NAME, appsMoreApps[i 
           - appsDashBoard.length].getActivityName()); 
         cv[i].put(AppsTable.COLUMN_APP_NAME, appsMoreApps[i 
           - appsDashBoard.length].getAppName()); 
         cv[i].put(AppsTable.COLUMN_IS_FROM_DASHBOARD, "0"); 
        } 
        return appsProvider.insertBulkApps(cv); 
    } 
    
    private void getDataFromDatabase() { 
    
        Cursor appCursor = appsProvider.getAllApps(); 
        appCursor.moveToFirst(); 
    
        for (int i = 0; i < appsDashBoard.length; i++) { 
         appsDashBoard[i] 
           .setData(
             appCursor.getString(appCursor 
               .getColumnIndex(AppsTable.COLUMN_APP_NAME)), 
             appCursor.getString(appCursor 
               .getColumnIndex(AppsTable.COLUMN_APP_ACTIVITY_NAME)), 
             appCursor.getString(appCursor 
               .getColumnIndex(AppsTable.COLUMN_APP_PACKAGE_NAME))); 
         appCursor.moveToNext(); 
        } 
    
        for (int i = 0; i < appsMoreApps.length; i++) { 
         appsMoreApps[i] 
           .setData(
             appCursor.getString(appCursor 
               .getColumnIndex(AppsTable.COLUMN_APP_NAME)), 
             appCursor.getString(appCursor 
               .getColumnIndex(AppsTable.COLUMN_APP_ACTIVITY_NAME)), 
             appCursor.getString(appCursor 
               .getColumnIndex(AppsTable.COLUMN_APP_PACKAGE_NAME))); 
         appCursor.moveToNext(); 
        } 
    } 
    
    private class saveDataTask extends AsyncTask<Void, Void, Integer> { 
    
        @Override 
        protected Integer doInBackground(Void... params) { 
         return saveDataInDatabase(); 
        } 
    
        @Override 
        protected void onPostExecute(Integer result) { 
    
         if (mListener != null) 
          mListener.onDataUpdate(TYPE_SAVE); 
         super.onPostExecute(result); 
        } 
    
    } 
    
    private class getDataTask extends AsyncTask<Void, Void, Void> { 
    
        @Override 
        protected Void doInBackground(Void... params) { 
         getDataFromDatabase(); 
         return null; 
        } 
    
        @Override 
        protected void onPostExecute(Void result) { 
         if (mListener != null) 
          mListener.onDataUpdate(TYPE_GET); 
         super.onPostExecute(result); 
        } 
    } 
    
    private class updateDataTask extends AsyncTask<Void, Void, Void> { 
    
        boolean isFromDashBoard; 
        int index; 
    
        public updateDataTask(boolean isFromDashBoard, int index) { 
         this.isFromDashBoard = isFromDashBoard; 
         this.index = index; 
    
        } 
    
        @Override 
        protected Void doInBackground(Void... params) { 
         updateAppInDatabase(isFromDashBoard, index); 
         return null; 
        } 
    
        @Override 
        protected void onPostExecute(Void result) { 
         if (mListener != null) 
          mListener.onDataUpdate(TYPE_UPDATE); 
         super.onPostExecute(result); 
        } 
    } 
    
    public static class AppsData { 
        int id; 
        String appName; 
        String activityName; 
        String packageName; 
    
        public AppsData(int id, String appName, String activityName, 
          String packageName) { 
         this.id = id; 
         this.appName = appName; 
         this.activityName = activityName; 
         this.packageName = packageName; 
        } 
    
        public void setData(String appName, String activityName, 
          String packageName) { 
         this.appName = appName; 
         this.activityName = activityName; 
         this.packageName = packageName; 
        } 
    
        public int getId() { 
         return id; 
        } 
    
        public String getAppName() { 
         return appName; 
        } 
    
        public String getPackageName() { 
         return packageName; 
        } 
    
        public String getActivityName() { 
         return activityName; 
        } 
    } 
    
    public interface OnDataBaseUpdateListener { 
        public void onDataUpdate(int updateType); 
    } 
    } 
    

它可以幫助您創建一個更有條理的代碼,並始終可用的數據,只需要調用AppsHelper.getInstance(context)和繁榮!!! :)

相關問題