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里面的问题, 菜鸟请教一个简单的问题.... 关于谷歌天气预报的API。。 关于 滑动的引导界面 紧急求助 原先SDK是4.0时没问题 换成2.3.1后运行时出现如下错误 关于Broadcast和BroadcastReceiver的疑问。。。。 android 添加CheckBox单击事件编译通过,但模拟器中报错,这是为什么? 触屏事件的获取 android 混音 谁能给我一份这样的demo 请问下Failed resolution of: [Ljava/beans/PropertyDescriptor该怎么解决呢? 谁有演示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里面的问题,