- 相關(guān)推薦
android面試題(2)
Intent intent = new Intent(this,B.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
局限性 :所有的activity的啟動(dòng)模式都要是默認(rèn)的啟動(dòng)模式
講一講你對(duì)activity的理解
把上面的幾點(diǎn)用自己的心得寫出來(lái)
8. service是否在main thread中執(zhí)行, service里面是否能執(zhí)行耗時(shí)的操作?
默認(rèn)情況,如果沒(méi)有顯示的指定service所運(yùn)行的進(jìn)程, Service和activity是運(yùn)行在當(dāng)前app所在進(jìn)程的main thread(UI主線程)里面
service里面不能執(zhí)行耗時(shí)的操作(網(wǎng)絡(luò)請(qǐng)求,拷貝數(shù)據(jù)庫(kù),大文件 )
在子線程中執(zhí)行 new Thread(){}.start();
特殊情況 ,可以在清單文件配置 service 執(zhí)行所在的進(jìn)程 ,讓service在另外的進(jìn)程中執(zhí)行
9. 兩個(gè)Activity之間怎么傳遞數(shù)據(jù)?
基本數(shù)據(jù)類型可以通過(guò). Intent 傳遞數(shù)據(jù)
extras.putDouble(key, value)
intent.putExtra(name, value)
// 通過(guò)intent putExtra 方法 基本數(shù)據(jù)類型 都傳遞
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 全局里面存放 對(duì)象 ,自己去實(shí)現(xiàn)自己的application的這個(gè)類,基礎(chǔ)系統(tǒng)的application , 每個(gè)activity都可以取到
讓對(duì)象實(shí)現(xiàn) implements Serializable 接口把對(duì)象存放到文件上.
讓類實(shí)現(xiàn)Serializable 接口,然后可以通過(guò) ObjectOutputStream //對(duì)象輸出流
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 把對(duì)象序列化到android操作系統(tǒng) 的一塊公用的內(nèi)存空間
文件/網(wǎng)絡(luò)
intent.setData(Uri)
Uri.fromFile(); //大圖片的傳遞
contentResolver.getInputStream(url);
10. 怎么讓在啟動(dòng)一個(gè)Activity是就啟動(dòng)一個(gè)service?
在activity的onCreate()方法里面 startService();
11. 同一個(gè)程序,但不同的Activity是否可以放在不同的Task任務(wù)棧中?
比方說(shuō)在激活一個(gè)新的activity時(shí)候, 給intent設(shè)置flag
Intent的flag添加FLAG_ACTIVITY_NEW_TASK
這個(gè)被激活的activity就會(huì)在新的task棧里面…
Intent intent = new Intent(A.this,B.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
12. Activity怎么和service綁定,怎么在activity中啟動(dòng)自己對(duì)應(yīng)的service?
startService() 一旦被創(chuàng)建 調(diào)用著無(wú)關(guān) 沒(méi)法使用service里面的方法
bindService () 把service 與調(diào)用者綁定 ,如果調(diào)用者被銷毀, service會(huì)銷毀
bindService() 我們可以使用service 里面的方法
bindService(). 讓activity能夠訪問(wèn)到 service里面的方法
構(gòu)建一個(gè)intent對(duì)象,
Intent service = new Intent(this,MyService.class);
通過(guò)bindService的方法去啟動(dòng)一個(gè)服務(wù),
bindService(intent, new MyConn(), BIND_AUTO_CREATE);
ServiceConnection 對(duì)象(重寫onServiceConnected和OnServiceDisconnected方法) 和BIND_AUTO_CREATE.
private class myconn implements ServiceConnection
{
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
//可以通過(guò)IBinder的對(duì)象 去使用service里面的方法
}
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
13. 14 .什么是Service以及描述下它的生命周期。Service有哪些啟動(dòng)方法,有什么區(qū)別,怎樣停用Service?
在Service的生命周期中,被回調(diào)的方法比Activity少一些,只有onCreate, onStart, onDestroy,
onBind和onUnbind。
通常有兩種方式啟動(dòng)一個(gè)Service,他們對(duì)Service生命周期的影響是不一樣的。
1 通過(guò)startService
Service會(huì)經(jīng)歷 onCreate 到onStart,然后處于運(yùn)行狀態(tài),stopService的時(shí)候調(diào)用onDestroy方法。
如果是調(diào)用者自己直接退出而沒(méi)有調(diào)用stopService的話,Service會(huì)一直在后臺(tái)運(yùn)行。
2 通過(guò)bindService
Service會(huì)運(yùn)行onCreate,然后是調(diào)用onBind, 這個(gè)時(shí)候調(diào)用者和Service綁定在一起。調(diào)用者退出了,Srevice就會(huì)調(diào)用onUnbind->onDestroyed方法。
所謂綁定在一起就共存亡了。調(diào)用者也可以通過(guò)調(diào)用unbindService方法來(lái)停止服務(wù),這時(shí)候Srevice就會(huì)調(diào)用onUnbind->onDestroyed方法。
【android面試題(2)】相關(guān)文章:
android面試題目09-08
Android工程師的面試題08-07
Android工程師面試題10-24
Microsoft面試題09-04
iOS面試題07-10
公司面試題09-12
hibernate面試題10-18
英語(yǔ)面試題精選06-13