最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

Service概念及用途:

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

Service生命周期 :

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

Service與Activity通信:

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

1、添加一個類,在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時,讓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此時調(diào)用程序設(shè)置里面可以看到Running Service有一個LService

點(diǎn)擊stopService

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

點(diǎn)擊unbindService

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

相關(guān)文章

  • Kotlin中日志的使用方法詳解

    Kotlin中日志的使用方法詳解

    這篇文章主要給大家介紹了關(guān)于Kotlin中日志的使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Android自定義SwipeRefreshLayout高仿微信朋友圈下拉刷新

    Android自定義SwipeRefreshLayout高仿微信朋友圈下拉刷新

    這篇文章主要以社交APP的BOSS微信為例,介紹了Android自定義SwipeRefreshLayout高仿微信朋友圈下拉刷新,感興趣的小伙伴們可以參考一下
    2016-07-07
  • Android實(shí)現(xiàn)俄羅斯方塊

    Android實(shí)現(xiàn)俄羅斯方塊

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)俄羅斯方塊游戲 ,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • Android RollPagerView實(shí)現(xiàn)輪播圖

    Android RollPagerView實(shí)現(xiàn)輪播圖

    這篇文章主要介紹了Android RollPagerView實(shí)現(xiàn)輪播圖的相關(guān)資料,這里提供實(shí)例來實(shí)現(xiàn)輪播圖的簡單實(shí)例,希望能幫助到大家,需要的朋友可以參考下
    2017-08-08
  • Android實(shí)現(xiàn)隱私政策彈窗與鏈接功能

    Android實(shí)現(xiàn)隱私政策彈窗與鏈接功能

    現(xiàn)在幾乎所有的應(yīng)用市場都要求應(yīng)用上架需要用戶協(xié)議/隱私政策,本篇內(nèi)容將介紹如何在APP內(nèi)植入一個隱私政策彈窗與鏈接,對Android隱私政策彈窗實(shí)現(xiàn)代碼感興趣的朋友跟隨小編一起看看吧
    2021-07-07
  • android自定義控件ImageView實(shí)現(xiàn)圓形圖片

    android自定義控件ImageView實(shí)現(xiàn)圓形圖片

    這篇文章主要為大家詳細(xì)介紹了android自定義控件ImageView實(shí)現(xiàn)圓形圖片,適用于用戶頭像,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Android如何修改默認(rèn)gradle路徑的方法

    Android如何修改默認(rèn)gradle路徑的方法

    Android Studio每次新建項(xiàng)目,都會默認(rèn)在C盤生成并下載gradle相關(guān)文件,那么Android如何修改默認(rèn)gradle路徑的方法,本文就來介紹一下
    2023-08-08
  • EditText監(jiān)聽方法,實(shí)時的判斷輸入多少字符

    EditText監(jiān)聽方法,實(shí)時的判斷輸入多少字符

    在EditText提供了一個方法addTextChangedListener實(shí)現(xiàn)對輸入文本的監(jiān)控。本文分享了EditText監(jiān)聽方法案例,需要的朋友一起來看下吧
    2016-12-12
  • Android自定義實(shí)現(xiàn)BaseAdapter的普通實(shí)現(xiàn)

    Android自定義實(shí)現(xiàn)BaseAdapter的普通實(shí)現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了Android自定義實(shí)現(xiàn)BaseAdapter的普通實(shí)現(xiàn),感興趣的小伙伴們可以參考一下
    2016-08-08
  • ImageSwitcher圖像切換器的使用實(shí)例

    ImageSwitcher圖像切換器的使用實(shí)例

    這篇文章主要為大家詳細(xì)介紹了ImageSwitcher圖像切換器的使用實(shí)例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-10-10

最新評論

镇原县| 霞浦县| 黔江区| 肇州县| 邳州市| 大英县| 彭阳县| 鸡西市| 古交市| 道孚县| 全州县| 台山市| 梓潼县| 琼海市| 大方县| 上杭县| 呈贡县| 嘉禾县| 南涧| 留坝县| 孝昌县| 吉林省| 霍山县| 渭南市| 唐河县| 大荔县| 博乐市| 镇赉县| 利川市| 青田县| 仪征市| 福泉市| 武威市| 伊宁县| 鹰潭市| 高平市| 启东市| 浮山县| 勃利县| 土默特右旗| 五河县|