getApplicationContext意味着一个程序只有一个上下文? 一个application有多个context包含的activity service等也会有自己的context 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这么看来,每个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(); } } } 上下文“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(); } } } 理解成唯一标识可能会更好一些类似window里面的handle getApplicationContext()方法名写的算清楚吧,application的context,其实我是觉得这是设计上的一个逻辑封装而已,你要是觉得它那个不够用或者不想用,你也可以写一个你业务上的context来封装你的业务,或者干脆把它的context整个丢到你的context里面,只不过是存在它的context如果有变化如何同步到你的context里面的问题, 安卓最新开发工具安装—Android Studio AnimationDrawable逐帧动画的一点问题 在/system/app下面的apk 求助: Android4.0触摸屏驱动正常但触摸无作用 我想查看每一个应用程序的耗电量您那有什么思路吗 Java.lang.IndexOutOfBoundException错误 android反编译 大家帮看看,这样的功能用下面哪种方法实现比较合理? 请问,关于杀死线程的问题 华为Android 8.0系统,后台服务无操作一段时间后无网络 谁有演示drm工作的简单的demo android客户端页卡问题
难怪,我看每个函数,将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();
}
}
}
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();
}
}
}
类似window里面的handle
方法名写的算清楚吧,application的context,
其实我是觉得这是设计上的一个逻辑封装而已,你要是觉得它那个不够用或者不想用,你也可以写一个你业务上的context来封装你的业务,或者干脆把它的context整个丢到你的context里面,只不过是存在它的context如果有变化如何同步到你的context里面的问题,