Android基础笔记一
来源:互联网 发布:lab series 知乎 编辑:程序博客网 时间:2024/05/23 20:21
Android组件Activity
1.onCreate(), onStart(), onResume(), onPause(), onStop(), onResult(), onDestory()
2.要想启动另一个Activity,必须在AndroidManifest.xml进行配置。在onCreate()函数中,把布局的xml文件用setContentView(R.layout.XXXX)引入,便可以用findViewById(R.id.XXX)来获取控件。之后便可以在获取到的控件上添加监听事件。
3.从MainActivity中向其他的Activity传递信息有两种方式:
btnStartAty1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubIntent i = new Intent(MainActivity.this, Aty1.class);//i.putExtra("txt", "Hello Acy1");Bundle data = new Bundle();data.putString("txt", "Hello Aty1");i.putExtras(data);//startActivity(i);startActivityForResult(i, 0);}});
第一种方式是,i.putExtra("txt", "Hello Aty1")。第二种方式是,传递一个Bundle。之后便可以在要传递给的Activity的onCreate()中,获取传递来的Hello Aty1
4.如果MainActivity中要获取其他的Activity中传递来的数据,那么在启动Activity时,不可以用startActivity(i),而是要用startActivityForResult(i, 0);
5.在主MainActivity中获取其余Activity传递来的数据时,在其余的Activity中的onCreate()中要,
btnClose.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubIntent i = new Intent();i.putExtra("txt", "Hello ManActivity"); //要传递的数据setResult(0, i); //传给MainActivityfinish();}});
在主MainActivity中,用如下的方法,接收传递来的数据。
public void onActivityResult(int requestCode, int resultCode, Intent data) { String result = data.getStringExtra("txt"); tvOut.setText(result); super.onActivityResult(requestCode, resultCode, data); }
1.启动service与关闭service
启动service时,要先建立一个service,然后在AndroidManifest.xml注册。
public class MainActivity extends Activity implements OnClickListener, ServiceConnection{private Button btnStartService, btnStopService, btnBindService, btnUnbindService, btnGetCurrentNum;private Intent serviceIntent;private EchoService echoService = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); serviceIntent = new Intent(this, EchoService.class); btnStartService = (Button) findViewById(R.id.btnStartService); btnStopService = (Button) findViewById(R.id.btnStopService); btnBindService = (Button) findViewById(R.id.btnBindService); btnUnbindService = (Button) findViewById(R.id.btnUnbindService); btnGetCurrentNum = (Button) findViewById(R.id.btnGetCurrentNum); btnStartService.setOnClickListener(this); btnStopService.setOnClickListener(this); btnBindService.setOnClickListener(this); btnUnbindService.setOnClickListener(this); btnGetCurrentNum.setOnClickListener(this); } @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; }@Overridepublic void onClick(View v) {switch(v.getId()) {case R.id.btnStartService:startService(serviceIntent);break;case R.id.btnStopService:stopService(serviceIntent);break;case R.id.btnBindService:bindService(serviceIntent, this, Context.BIND_AUTO_CREATE);break;case R.id.btnUnbindService:unbindService(this);echoService = null;break;case R.id.btnGetCurrentNum:if (echoService != null) {System.out.println("now service number is = "+echoService.getCurrentNum());}}}@Overridepublic void onServiceConnected(ComponentName arg0, IBinder binder) {System.out.println("onServiceConnect");echoService = ((EchoService.EchoServiceBinder)binder).getService();}@Overridepublic void onServiceDisconnected(ComponentName arg0) {// TODO Auto-generated method stub} }
public class EchoService extends Service {@Overridepublic IBinder onBind(Intent arg0) {// TODO Auto-generated method stubSystem.out.println("onBind");return echoServiceBinder;}public final EchoServiceBinder echoServiceBinder = new EchoServiceBinder();public class EchoServiceBinder extends Binder {public EchoService getService() {return EchoService.this;}}public int getCurrentNum() {return i;}@Overridepublic void onCreate() {super.onCreate();System.out.println("onCreate");startTimer();}@Overridepublic void onDestroy() {super.onDestroy();System.out.println("onDestroy");stopTimer();}private int i = 0;public void startTimer() {if(timer == null) {timer = new Timer();task = new TimerTask() {@Overridepublic void run() {// TODO Auto-generated method stubi++;System.out.println(i);}};timer.schedule(task, 1000, 1000);}}public void stopTimer() {if (timer != null) {task.cancel();timer.cancel();task = null;timer = null;}}private Timer timer = null;private TimerTask task = null;}
btnStartService.setOnClickListener(this)。this是指处理监听事件的主体。函数onClick()中startService(serviceIntent)和stopService(serviceIntent)分别是启动服务和停止服务的代码。
启动服务时调用服务类中onCreate()函数,结束服务时调用onDestroy()函数。
2.绑定服务和解除服务
case R.id.btnBindService:bindService(serviceIntent, this, Context.BIND_AUTO_CREATE);break;case R.id.btnUnbindService:unbindService(this);echoService = null;break;绑定服务和解除服务如上代码所示。
绑定服务时,在自己定义的Service类里,会自动调用onBind函数。该函数若返回一个Binder的子类,便绑定成功。此时,在MainActivity中会自动调用onServiceConnected()函数。。通过在自己定义的服务类里返回Binder的子类,在定义Binder子类的代码里,返回该服务类的实例。那么在MainActivity里便可以取到该服务的实例,之后便可以调用该服务实例里的方法和属性。
- Android基础笔记一
- Android基础笔记(一)
- Android-基础笔记-HTTP (一)
- Android学习笔记一基础
- Android学习笔记一之Android基础
- Android学习笔记一之Android基础
- android学习笔记一(基础部分)
- android学习笔记一(基础篇)
- Android基础学习笔记(一)
- android学习笔记一(基础部分)
- Android基础笔记(一)-快速入门
- android RecylearView笔记(一)基础用法
- android NFC学习笔记(基础一)
- android基础学习笔记一(logcat)
- android学习笔记一 ---- android-sdk基础介绍
- Android学习笔记十之Android基础UI一
- Android学习笔记十之Android基础UI一
- android 学习笔记(一)基础篇(完善中)
- Android 自定义背景文件,android:shape的用法
- WebView的简单用法
- flex-iframe移动,以及关闭问题
- CrossApp类结构图
- 网络连接错误提示
- Android基础笔记一
- 编程能力与编程年龄
- 阿拉伯数字转大写金额(支持简体和繁体)
- Java过滤器与SpringMVC拦截器之间的关系与区别
- leetcode:Rotate List
- 搭建eclipse+codesourcery的STM32开发环境
- java ThreadLocal深入解析
- FDHGJNGFJFGJ
- DSFJGHGKLJH.;