android面试题(2)

学人智库 时间:2018-02-10 我要投稿
【www.unjs.com - 学人智库】

  Intent intent = new Intent(this,B.class);

  intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

  局限性 :所有的activity的启动模式都要是默认的启动模式

  讲一讲你对activity的理解

  把上面的几点用自己的心得写出来

  8. service是否在main thread中执行, service里面是否能执行耗时的操作?

  默认情况,如果没有显示的指定service所运行的进程, Service和activity是运行在当前app所在进程的main thread(UI主线程)里面

  service里面不能执行耗时的操作(网络请求,拷贝数据库,大文件 )

  在子线程中执行 new Thread(){}.start();

  特殊情况 ,可以在清单文件配置 service 执行所在的进程 ,让service在另外的进程中执行

  9. 两个Activity之间怎么传递数据?

  基本数据类型可以通过. Intent 传递数据

  extras.putDouble(key, value)

  intent.putExtra(name, value)

  // 通过intent putExtra 方法 基本数据类型 都传递

  Bundle bundle = new Bundle();

  bumdle.putShort(key, value);

  intent.putExtras(bundle);

  intent.putExtras(bundle)

  获取到激活他的 getIntent();

  Intent intent = getIntent();

  Bundle bundle = intent.getExtras();

  intent.getStringExtra("key","value");

  intent.getBooleanExtra("key","value")

  Application 全局里面存放 对象 ,自己去实现自己的application的这个类,基础系统的application , 每个activity都可以取到

  让对象实现 implements Serializable 接口把对象存放到文件上.

  让类实现Serializable 接口,然后可以通过 ObjectOutputStream //对象输出流

  File file = new File("c:\\1.obj");

  FileOutputStream fos = new FileOutputStream(file);

  ObjectOutputStream oos = new ObjectOutputStream(fos);

  Student stu = new Student();

  stu.setId("10001");

  stu.setName("zs");

  oos.writeObject(stu);

  FileInputStream fis = new FileInputStream(file);

  ObjectInputStream ois = new ObjectInputStream(fis);

  Student stu1 = (Student) ois.readObject();

  System.out.println(stu1.getName());

  Parcelable 和 Serializable

  Parcelable 把对象序列化到android操作系统 的一块公用的内存空间

  文件/网络

  intent.setData(Uri)

  Uri.fromFile(); //大图片的传递

  contentResolver.getInputStream(url);

  10. 怎么让在启动一个Activity是就启动一个service?

  在activity的onCreate()方法里面 startService();

  11. 同一个程序,但不同的Activity是否可以放在不同的Task任务栈中?

  比方说在激活一个新的activity时候, 给intent设置flag

  Intent的flag添加FLAG_ACTIVITY_NEW_TASK

  这个被激活的activity就会在新的task栈里面…

  Intent intent = new Intent(A.this,B.class);

  intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

  startActivity(intent);

  12. Activity怎么和service绑定,怎么在activity中启动自己对应的service?

  startService() 一旦被创建 调用着无关 没法使用service里面的方法

  bindService () 把service 与调用者绑定 ,如果调用者被销毁, service会销毁

  bindService() 我们可以使用service 里面的方法

  bindService(). 让activity能够访问到 service里面的方法

  构建一个intent对象,

  Intent service = new Intent(this,MyService.class);

  通过bindService的方法去启动一个服务,

  bindService(intent, new MyConn(), BIND_AUTO_CREATE);

  ServiceConnection 对象(重写onServiceConnected和OnServiceDisconnected方法) 和BIND_AUTO_CREATE.

  private class myconn implements ServiceConnection

  {

  public void onServiceConnected(ComponentName name, IBinder service) {

  // TODO Auto-generated method stub

  //可以通过IBinder的对象 去使用service里面的方法

  }

  public void onServiceDisconnected(ComponentName name) {

  // TODO Auto-generated method stub

  }

  13. 14 .什么是Service以及描述下它的生命周期。Service有哪些启动方法,有什么区别,怎样停用Service?

  在Service的生命周期中,被回调的方法比Activity少一些,只有onCreate, onStart, onDestroy,

  onBind和onUnbind。

  通常有两种方式启动一个Service,他们对Service生命周期的影响是不一样的。

  1 通过startService

  Service会经历 onCreate 到onStart,然后处于运行状态,stopService的时候调用onDestroy方法。

  如果是调用者自己直接退出而没有调用stopService的话,Service会一直在后台运行。

  2 通过bindService

  Service会运行onCreate,然后是调用onBind, 这个时候调用者和Service绑定在一起。调用者退出了,Srevice就会调用onUnbind->onDestroyed方法。

  所谓绑定在一起就共存亡了。调用者也可以通过调用unbindService方法来停止服务,这时候Srevice就会调用onUnbind->onDestroyed方法。