一个application有多个context
包含的activity service等也会有自己的context

解决方案 »

  1.   

    这么看来,每个activity或service,都有唯一的一个自身context!而且可以用此函数来获得引用。
    难怪,我看每个函数,将context作为一个参数传入。
    public void saveContent(Context context, String fileName, String content) {
            boolean bPermission = isHavePermission(context, "android.permission.WRITE_EXTERNAL_STORAGE");
            Log.d(TAG, "saveContent() bPermission = "+bPermission);
            if (!bPermission) {
                return;
            }        String path = getMsgPath(context) + fileName + ".txt";
            FileOutputStream fileOutput = null;
            Log.d(TAG, "saveContent() file=" + path);        try {
                File file = new File(path);
                if (!file.exists()) {
                    file.createNewFile();
                    Log.d(TAG, "saveContent() create new file");
                }
                fileOutput = new FileOutputStream(file);
                fileOutput.write(content.getBytes());
                fileOutput.flush();            fileOutput.close();
                fileOutput = null;            Log.d(TAG, "saveContent() save file succeed");
                return;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (null != fileOutput) {
                        fileOutput.close();
                        fileOutput = null;
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } 
      

  2.   

    上下文“context”,是不是可以理解为一个容器?activity/server的。
    public void saveContentSD(Context context, String fileName, String content) {
            boolean bPermission = isHavePermission(context, "android.permission.WRITE_EXTERNAL_STORAGE");
            Log.d(TAG, "saveContentSD() bPermission = "+bPermission);
            if (!bPermission) {
                return;
            }        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
            format.setTimeZone(TimeZone.getDefault());
            String time = format.format(new Date(System.currentTimeMillis()));
            String path = getMsgPathSD(context) + (time + "_" + fileName) + ".txt";
            Log.d(TAG, "saveContentSD() data file path:" + path);
            File file = new File(path);
            FileOutputStream fileOutput = null;        try {
                if (!file.exists()) {
                    file.createNewFile();
                    Log.d(TAG, "saveContentSD() create file filename");
                }
                fileOutput = new FileOutputStream(file);
                fileOutput.write(content.getBytes());
                fileOutput.flush();            fileOutput.close();
                fileOutput = null;            Log.d(TAG, "saveContentSD() save:" + file.getPath());
                return;
            } catch (FileNotFoundException e) {
                Log.d(TAG, "saveContentSD() error:" + e.getMessage());
                e.printStackTrace();
            } catch (IOException e) {
                Log.d(TAG, "saveContentSD() error:" + e.getMessage());
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (null != fileOutput) {
                        fileOutput.close();
                        fileOutput = null;
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
      

  3.   

    理解成唯一标识可能会更好一些
    类似window里面的handle
      

  4.   

    getApplicationContext()
    方法名写的算清楚吧,application的context,
    其实我是觉得这是设计上的一个逻辑封装而已,你要是觉得它那个不够用或者不想用,你也可以写一个你业务上的context来封装你的业务,或者干脆把它的context整个丢到你的context里面,只不过是存在它的context如果有变化如何同步到你的context里面的问题,