亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

基于Android Service 生命周期的詳細(xì)介紹

 更新時(shí)間:2013年04月21日 09:14:51   作者:  
本篇文章小編為大家介紹,基于Android Service 生命周期的詳解。需要的朋友參考下

Service概念及用途:

Android中的服務(wù),它與Activity不同,它是不能與用戶交互的,不能自己啟動(dòng)的,運(yùn)行在后臺的程序,如果我們退出應(yīng)用時(shí),Service進(jìn)程并沒有結(jié)束,它仍然在后臺運(yùn)行,那我們什么時(shí)候會用到Service呢?比如我們播放音樂的時(shí)候,有可能想邊聽音樂邊干些其他事情,當(dāng)我們退出播放音樂的應(yīng)用,如果不用Service,我們就聽不到歌了,所以這時(shí)候就得用到Service了,又比如當(dāng)我們一個(gè)應(yīng)用的數(shù)據(jù)是通過網(wǎng)絡(luò)獲取的,不同時(shí)間(一段時(shí)間)的數(shù)據(jù)是不同的這時(shí)候我們可以用Service在后臺定時(shí)更新,而不用每打開應(yīng)用的時(shí)候在去獲取。

Service生命周期 :

Android Service的生命周期并不像Activity那么復(fù)雜,它只繼承了onCreate(),onStart(),onDestroy()三個(gè)方法,當(dāng)我們第一次啟動(dòng)Service時(shí),先后調(diào)用了onCreate(),onStart()這兩個(gè)方法,當(dāng)停止Service時(shí),則執(zhí)行onDestroy()方法,這里需要注意的是,如果Service已經(jīng)啟動(dòng)了,當(dāng)我們再次啟動(dòng)Service時(shí),不會在執(zhí)行onCreate()方法,而是直接執(zhí)行onStart()方法。

Service與Activity通信:

Service后端的數(shù)據(jù)最終還是要呈現(xiàn)在前端Activity之上的,因?yàn)閱?dòng)Service時(shí),系統(tǒng)會重新開啟一個(gè)新的進(jìn)程,這就涉及到不同進(jìn)程間通信的問題了(AIDL),當(dāng)我們想獲取啟動(dòng)的Service實(shí)例時(shí),我們可以用到bindServiceunBindService方法,它們分別執(zhí)行了Service中IBinder()和onUnbind()方法。

1、添加一個(gè)類,在MainActivity所在包之下

復(fù)制代碼 代碼如下:

public class LService extends Service {
 private static final String TAG = "LService";
 @Override
 public IBinder onBind(Intent intent) {
  Log.i(TAG, "onbind");
  return null;
 }
 @Override
 public void onCreate() {
  Log.i(TAG, "oncreate");
  super.onCreate();
 }
 @Override
 public void onStart(Intent intent, int startId) {
  Log.i(TAG, "onstart");
  super.onStart(intent, startId);
 }
 @Override
 public void onDestroy() {
  Log.i(TAG, "ondestoty");
  super.onDestroy();
 }
 @Override
 public boolean onUnbind(Intent intent) {
  Log.i(TAG, "onubind");
  return super.onUnbind(intent);
 }
 public String getSystemTime() {
  Time t = new Time();
  t.setToNow();
  return t.toString();
 }
 public class LBinder extends Binder {
  LService getService() {
   return LService.this;
  }
 }
}



 2、在程序界面文件中添加控件
復(fù)制代碼 代碼如下:

<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="wecclome to Livingstone&apos;s bolg" />

<Button
android:id="@+id/startservice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="startService" />

<Button
android:id="@+id/stopservice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="stopService" />

<Button
android:id="@+id/bindservice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="bindService" />

<Button
android:id="@+id/unbindservice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="unbindService" />


3、修改MainActivity中的方法,以及讓MainActivity類實(shí)現(xiàn)OnClickListener接口
復(fù)制代碼 代碼如下:

public class MainActivity extends Activity implements OnClickListener {
 private LService mLService;
 private TextView mTextView;
 private Button startServiceButton;
 private Button stopServiceButton;
 private Button bindServiceButton;
 private Button unbindServiceButton;
 private Context mContext;
 // 這里需要用到ServiceConnection,在Context.bindService和context.unBindService()里用到
 private ServiceConnection mServiceConnection = new ServiceConnection() {
  // 當(dāng)bindService時(shí),讓TextView顯示LService里getSystemTime()方法的返回值
  @Override
  public void onServiceConnected(ComponentName name, IBinder service) {
   mLService = ((LService.LBinder) service).getService();
   mTextView.setText("I am from Service :" + mLService.getSystemTime());
  }
  public void onServiceDisconnected(ComponentName name) {
  }
 };
 public void setupViews() {
  mContext = MainActivity.this;
  mTextView = (TextView) findViewById(R.id.text);

  startServiceButton = (Button) findViewById(R.id.startservice);
  stopServiceButton = (Button) findViewById(R.id.stopservice);
  bindServiceButton = (Button) findViewById(R.id.bindservice);
  unbindServiceButton = (Button) findViewById(R.id.unbindservice);

  startServiceButton.setOnClickListener(this);
  stopServiceButton.setOnClickListener(this);
  bindServiceButton.setOnClickListener(this);
  unbindServiceButton.setOnClickListener(this);
 }
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  setupViews();
 }
 @Override
 public void onClick(View v) {
  if (v == startServiceButton) {
   Intent i = new Intent(MainActivity.this, LService.class);
   mContext.startService(i);
  } else if (v == stopServiceButton) {
   Intent i = new Intent(MainActivity.this, LService.class);
   mContext.stopService(i);
  } else if (v == bindServiceButton) {
   Intent i = new Intent(MainActivity.this, LService.class);
   mContext.bindService(i, mServiceConnection, BIND_AUTO_CREATE);
  } else {
   mContext.unbindService(mServiceConnection);
  }
 }
}


4、注冊Service

<service
  android:name=".LService"
  android:exported="true" >
</service>

5、運(yùn)行程序

程序界面

點(diǎn)擊startService此時(shí)調(diào)用程序設(shè)置里面可以看到Running Service有一個(gè)LService

點(diǎn)擊stopService

點(diǎn)擊bindService此時(shí)Service已經(jīng)被關(guān)閉

點(diǎn)擊unbindService

先點(diǎn)擊startService,再依次點(diǎn)擊bindService和unbindService

相關(guān)文章

最新評論