2015年3月26日 星期四

[轉] 反彙編android的「boot.img」鏡像裡的「kernel」文件的方法

第一步:解壓boot.img,從而得到 文件「kernel」
             在windows中解壓boot.img的方法請參考: http://www.anzhuo2.com/thread-4391-1-1.html
             在linux中的方法,請參考:http://bluedrum.sinaapp.com/archives/202.html

第二步:使用objdump反彙編,dump 「kernel」文件,得到彙編代碼。

使用arm-linux 工具鏈裡面的arm-linux-objdump 就能反彙編
cd到bin文件所在的目錄, 在命令行下輸入:
arm-linux-objdump -D -b binary -m arm xxx.bin > xxx.asm
參數:
-D 反編譯所有代碼
-m 主機類型, arm
-b 文件格式, binary
對於ELF格式的文件只要一個-D參數即可
就可以把xxx.bin反彙編到xxx.asm文件

如果你有android的源碼和開發環境,可以使用預編譯目錄下的「arm-eabi-objdump」
如下: # androidroot/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin./arm-eabi-objdump -D -b binary -m arm ~/kernel >~/armkernel.asm

2015年3月5日 星期四

[轉] Android: startService vs bindService

http://blog.kenyang.net/2012/11/android-startservice-vs-bindservice.html

這篇要說明
  • startService
  • bindService
這兩者的差別!

其實在我之前的筆記有關Service的文章,都是使用startService!
並沒有使用bindService!
一方面是因為startService簡單很多!
再來也是最近才發現有bindService!
應該是說我原本以為startService的行為與bindService差不多!
但實際找答案以後才知道不是這樣的!


這裡就不多作說明如何使用startService或者bindService!
主要會說明這兩者差異性!
如果要看如何使用可以看之前的筆記:

  1. Android C2DM (四):實作之Android篇
  2. android detect screen on and screen off
  3. android 偵測 sdcard


先去看Google的定義,對於startService,Google定義如下
Once started, a service can run in the background indefinitely, even if the component that started it is destroyed. Usually, a started service performs a single operation and does not return a result to the caller. For example, it might download or upload a file over the network. When the operation is done, the service should stop itself.
簡言之用startService啟動,這個service是不會跟隨著啓動他的component消滅!且原則上是不能與UI互動的!(原則上)


而對於bindService,定義如下:
A bound service offers a client-server interface that allows components to interact with the service, send requests, get results, and even do so across processes with interprocess communication (IPC). A bound service runs only as long as another application component is bound to it. Multiple components can bind to the service at once, but when all of them unbind, the service is destroyed.
用bindService起來的話,則此service可以跟該component進行溝通!甚至可以做到 IPC!

小結:
bindService和startService最主要的差異在於其本身的lifecycle!
以及bindService可以用來做IPC!簡單的說就是可以讓你的service和UI溝通!
這裡就用一個project來說明吧!

先建立一個new project!
在這project中建立一個Activity,並指定它為launcher!
然後在建立一個Service!內如如下:
01public class Services extends Service {
02 
03 @Override
04 public IBinder onBind(Intent intent) {
05  // TODO Auto-generated method stub
06  Log.d("TestService","onBind");
07  return null;
08 }
09  
10 @Override
11 public int onStartCommand(Intent intent, int flags, int startId) {
12  Log.d("TestService","onStartCommand");
13  return super.onStartCommand(intent, flags, startId);
14 }
15 @Override
16 public ComponentName startService(Intent service) {
17  Log.d("TestService","startService");
18  return super.startService(service);
19 }
20  
21 @Override
22 public void onCreate() {
23  super.onCreate();
24  Log.d("TestService","onCreate");
25 }
26  
27 @Override
28 public void onDestroy() {
29  super.onDestroy();
30  Log.d("TestService","onDestroy");
31 }
32 
33 @Override
34 public boolean onUnbind(Intent intent) {
35  Log.d("TestService","onUnbind");
36  return super.onUnbind(intent);
37 }
38  
39}

可以看到上面的Service中並沒有做任何處理!
只有簡單的Log!
目的只是要讓我們瞭解startService和bindService的lifecycle差異性!

首先先來講startService,
我們先在onCreate中start!如下:
1@Override
2 protected void onCreate(Bundle savedInstanceState) {
3  super.onCreate(savedInstanceState);
4   
5  startService(new Intent(this,Services.class));
6 }

然後按下run,在開啟DDMS來觀看log!
會發現印出了”onCreate"->"onStartCommand"!

這時候按下back鍵來destroy這個activity,你會發現什麼都沒有印出來!
也就是說這個service仍在background執行!(你得手動stop)

這時候我們換成bindService來看看,如下:
1@Override
2 protected void onCreate(Bundle savedInstanceState) {
3  super.onCreate(savedInstanceState);
4   
5  bindService(new Intent(this,Services.class),null,Context.BIND_AUTO_CREATE);
6 }

一樣按下run!再看一下LOG!
會發現印出“onCreate"->"onBind"!

再按下back鍵,會發現...印出了!
“onUnbind"->"onDestroy"



總結上面兩項的差異,
會發現使用bindService時,
這個service的lifecycle是跟隨著bind它的object!
所以當該object被destroy,自然而然這個service也被destroy!

而使用startService,
就不是這樣了!這個service就是獨立的!不屬於任何一個object!
得靠stopService來停止!(任何一個Activity中都可以stop)


註:bindService的使用方式比較特別!
多帶了幾個參數!
在這個例子我們第二個參數設為null!
其實這參數是要傳入一個ServiceConnection的物件!
而ServiceConnection是一個Interface,這個Interface中
會有兩個callback function!
主要是用來monitor 這個service的state!