Context中文理解为上下文,程序运行的一个环境。
以下是SDK做出的解释:
Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.
英语很差,见笑了。
上下文就是提供关于应用环境的全局信息的东东,它的实现是服务于android系统的一个抽线类,它允许访问应用特定的资源个类,就像上调应用级别的操作,启动activity,启动广播,启动服务等。
在此我特意想了解Activity的上下文的生命周期,所以我就尝试去测试一下。
但是结果不是很满意。
1.用Activity不能启动内嵌的Service.
可能是Service还是需要注册进系统的,简单实例化Service是无效的。
public class MainActivity extends Activity { ....... statService(){ Intent = new Intent(this,MyService.getClasse()); startService(intent); } Service MyService = new Service(){ @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); Log.e("MyService", "onCreate"); new Thread(){ @Override public void run() { // TODO Auto-generated method stub super.run(); } }.start(); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); Log.e("MyService", "onDestroy"); } @Override public void onStart(Intent intent, int startId) { // TODO Auto-generated method stub super.onStart(intent, startId); Log.e("MyService", "onStart"); } }; }
2.试图在Activity用Handler对线程进行控制,(笔者yy:Activity打开的时候,点击开始按钮用Handler把Runnable给Post出去,线程就启动了,点击结束就调用Handler的removeCallback移除Runnable,理想状态是当我关闭Activity的时候,下次在进去点击结束还可以结束掉正在运行的线程,但是结果表明这是不行的,因为新创建的Activity里的Runnable并不是正在运行线程的runnable,况且Handler不是当初执行线程的那个Handler说不定也不能结束掉线程,笔者认为Handler跟post出去的线程是绑定的)
public Context context; private Handler handler = new Handler(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); context = this; initView (); }
case R.id.Start: handler.post(runable); break; case R.id.End: handler.removeCallbacks(runable);
private Runnable runable = new Runnable(){ @Override public void run() { // TODO Auto-generated method stub if(context!=null){ Log.e("Activity的上下文", "还在"); }else{ Log.e("Activity的上下文", "不存在了"); } handler.postDelayed(this, 1000); } };
3.杯具的事照样发生(本来想按回车键,退出Activity,用长期线程里去判断Activity的Context是否因退出Activity而销毁,或者启动另一个Activity或Service后调用finish()方法结束Activity,可是我看的事:图片)
boolean isServiceRun; @Override public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()){ case R.id.Start: handler.post(runable); break; case R.id.End: handler.removeCallbacks(runable); break; case R.id.Act: Intent intent = new Intent(this,OtherActivity3.class); context.startActivity(intent); break; case R.id.Svs: Intent service = new Intent(this,PageService.class); if(isServiceRun){ ((Button)findViewById(R.id.Svs)).setText("中止服务"); context.stopService(service); isServiceRun = false; finish(); }else{ ((Button)findViewById(R.id.Svs)).setText("开启服务"); context.startService(service); isServiceRun = true; finish(); } break; } }
疑惑!!!网上传Activity的上下文是随着Acitvity的创建而创建,销毁而销毁,难道Activity销毁了,线程还在跑就不算销毁?期待高手给我个答案!