context的作用:访问全局的资源的
访问资源的实例代码:
public class MainActivity extends Activity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//举例访问string.xml中的资源
// //setContentView(R.layout.activity_main);
// tv=new TextView(this);
// //tv.setText("Hello Android");
// //内部的实现: setText(getContext().getResources().getText(resid));
// tv.setText(R.string.hello_world);
// Toast.makeText(MainActivity.this,getResources().getText(R.string.hello_world) , Toast.LENGTH_SHORT).show();
// setContentView(tv);
//举例访问图片
ImageView iv=new ImageView(this);
iv.setImageResource(R.drawable.ic_launcher);
setContentView(iv);
}
通过以上的了解,我们可以知道,context可以作为全局资源共享的桥梁。
application才是真正的全局的上下文对象。
第一步:自己编写一个Application
package com.example.learncontext;
import android.app.Application;
public class App extends Application {
private String textData="app default";
public String getTextData() {
return textData;
}
public void setTextData(String textData) {
this.textData = textData;
}
第二步:FirstMainActivity
布局:
package com.example.learncontext;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class FirstMainActivity extends Activity {
private TextView textView;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_main);
textView=(TextView) findViewById(R.id.textView1);
editText=(EditText) findViewById(R.id.editText1);
textView.setText("共享的数据是:"+getApp().getTextData());
findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((App)getApplicationContext()).setTextData(editText.getText().toString());
textView.setText("共享的数据是:"+editText.getText().toString());
}
});
}
public App getApp(){
return (App) getApplicationContext();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
第三步:SecondMainActivity
布局:
package com.example.learncontext;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class SecondMainActivity extends Activity {
private TextView textView;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_main);
textView=(TextView) findViewById(R.id.textView1);
editText=(EditText) findViewById(R.id.editText1);
textView.setText("共享的数据是:"+getApp().getTextData());
findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((App)getApplicationContext()).setTextData(editText.getText().toString());
textView.setText("共享的数据是:"+editText.getText().toString());
}
});
}
public App getApp(){
return (App) getApplicationContext();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
第四部:
达到的效果:
AndroidManifest.xml:每个activity都必须在这个文件中进行注册,并且这个文件每个activity进行设置。
Application的生命周期:
在程序的启动时,肯定会用一些初始化,
package com.example.learncontext;
import android.app.Application;
import android.content.res.Configuration;
import android.widget.Toast;
public class App extends Application {
@Override
//这个application的onCreate的执行是在activity的onCreate方法之前。
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Toast.makeText(App.this, "App onCreate", Toast.LENGTH_SHORT).show();
}
@Override
public void onTerminate() {
// TODO Auto-generated method stub
super.onTerminate();
Toast.makeText(App.this, "App onTerminate", Toast.LENGTH_SHORT).show();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
Toast.makeText(App.this, "App onConfigurationChanged", Toast.LENGTH_SHORT).show();
}
@Override
public void onTrimMemory(int level) {
// TODO Auto-generated method stub
super.onTrimMemory(level);
Toast.makeText(App.this, "App onTrimMemory", Toast.LENGTH_SHORT).show();
}
private String textData="app default";
public String getTextData() {
return textData;
}
public void setTextData(String textData) {
this.textData = textData;
}
}