Context mContext = getApplicationContext();

本文解释了 Android 中 Context 的概念及不同获取方式的区别:通过 this 获取表明当前类是 Context 的子类,一般为 Activity 或 Application;通过 getApplicationContext 获取的是当前 App 所使用的 Application 对象,在任意位置调用该函数均可获得同一实例;而通过 getContext 获取的是当前对象所在的 Context。此外,还讨论了 Context 在实际应用中的意义。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Context mContext = getApplicationContext();其中这个是没有theme的配置的,所以应该都是用的默认的theme的配置。

使用this, 说明当前类是context的子类,一般是activity application等
使用getApplicationContext 取得的是当前app所使用的application,这在AndroidManifest中唯一指定。意味着,在当前app的任意位置使用这个函数得到的是同一个Context
使用getContext获取的是当前对象所在的Context
Context通常翻译成上下文,我通常当成场景来理解。

public class MainActivity extends AppCompatActivity implements View.OnClickListener { private EditText editname; private EditText editdetail; private Button btnsave; private Button btnclean; private Button btnread; private Context mContext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = getApplicationContext(); bindViews(); } private void bindViews() { editdetail = (EditText) findViewById(R.id.editdetail); editname = (EditText) findViewById(R.id.editname); btnclean = (Button) findViewById(R.id.btnclean); btnsave = (Button) findViewById(R.id.btnsave); btnread = (Button) findViewById(R.id.btnread); btnclean.setOnClickListener(this); btnsave.setOnClickListener(this); btnread.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btnclean: editdetail.setText(""); editname.setText(""); break; case R.id.btnsave: FileHelper fHelper = new FileHelper(mContext); String filename = editname.getText().toString(); String filedetail = editdetail.getText().toString(); try { fHelper.save(filename, filedetail); Toast.makeText(getApplicationContext(), "数据写入成功", Toast.LENGTH_SHORT).show(); } catch (Exception e) { e.printStackTrace(); Toast.makeText(getApplicationContext(), "数据写入失败", Toast.LENGTH_SHORT).show(); } break; case R.id.btnread: String detail = ""; FileHelper fHelper2 = new FileHelper(getApplicationContext()); try { String fname = editname.getText().toString(); detail = fHelper2.read(fname); } catch (IOException e) { e.printStackTrace(); } Toast.makeText(getApplicationContext(), detail, Toast.LENGTH_SHORT).show(); break; } } }public class FileHelper { private Context mContext; public FileHelper() { } public FileHelper(Context mContext) { super(); this.mContext = mContext; } /* * 这里定义的是一个文件保存的方法,写入到文件中,所以是输出流 * */ public void save(String filename, String filecontent) throws Exception { //这里我们使用私有模式,创建出来的文件只能被本应用访问,还会覆盖原文件哦 FileOutputStream output = mContext.openFileOutput(filename, Context.MODE_PRIVATE); output.write(filecontent.getBytes()); //将String字符串以字节流的形式写入到输出流中 output.close(); //关闭输出流 } /* * 这里定义的是文件读取的方法 * */ public String read(String filename) throws IOException { //打开文件输入流 FileInputStream input = mContext.openFileInput(filename); byte[] temp = new byte[1024]; StringBuilder sb = new StringBuilder(""); int len = 0; //读取文件内容: while ((len = input.read(temp)) > 0) { sb.append(new String(temp, 0, len)); } //关闭输入流 input.close(); return sb.toString(); } }转成kotlin
最新发布
07-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值