a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 系統 > Android > 正文

基于Android Service 生命周期的詳細介紹

2020-04-11 12:28:57
字體:
來源:轉載
供稿:網友

Service概念及用途:

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

Service生命周期 :

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

Service與Activity通信:

Service后端的數據最終還是要呈現在前端Activity之上的,因為啟動Service時,系統會重新開啟一個新的進程,這就涉及到不同進程間通信的問題了(AIDL),當我們想獲取啟動的Service實例時,我們可以用到bindServiceunBindService方法,它們分別執行了Service中IBinder()和onUnbind()方法。

1、添加一個類,在MainActivity所在包之下

復制代碼 代碼如下:

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、在程序界面文件中添加控件
復制代碼 代碼如下:

<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="wecclome to Livingstone'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類實現OnClickListener接口
復制代碼 代碼如下:

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() {
  // 當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、運行程序

程序界面

點擊startService此時調用程序設置里面可以看到Running Service有一個LService

點擊stopService

點擊bindService此時Service已經被關閉

點擊unbindService

先點擊startService,再依次點擊bindService和unbindService

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 久久免费精品 | 国产精品三级在线 | 亚洲一区在线视频 | 成人a在线| 日韩专区在线播放 | 一区小视频 | 1区2区视频 | 亚洲一区二区精品视频 | 国产精品久久一区二区三区 | 国产精品不卡一区 | 日韩一区二区免费视频 | 国产精品久久久久久亚洲调教 | 91精品国产99久久久久久红楼 | 中文字幕日韩久久 | 狠狠操天天干 | 久久精品亚洲欧美日韩精品中文字幕 | 制服 丝袜 激情 欧洲 亚洲 | 日韩中文字幕免费观看 | 欧美亚洲专区 | 久久久免费视频播放 | 日本高清网站 | 三级网站大全 | 粉嫩高清一区二区三区精品视频 | 日韩精品视频在线播放 | 亚洲第一免费视频网站 | 亚洲精品久久久一区二区三区 | 成人欧美一区二区三区在线播放 | 久久久www成人免费精品 | 视频一区二区国产 | 午夜激情免费 | 97成人在线视频 | a在线天堂 | 亚洲国产91 | 国产成人精品一区二区三区视频 | 一区久久 | 欧美日韩精品综合 | 中文字幕视频在线播放 | 日本久久精品视频 | 久热久热 | 先锋资源中文字幕 | 99精品欧美一区二区三区综合在线 |