Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了基于Android Service 生命周期的详细介绍大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

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、@L_479_29@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 @R_616_8313@ {
  }
 };
 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、运行程序

基于Android Service 生命周期的详细介绍程序界面

点击startservice

基于Android Service 生命周期的详细介绍此时

调用程序设置里面可以看到Running service有一个Lservice

点击stopservice

基于Android Service 生命周期的详细介绍

点击bindservice

基于Android Service 生命周期的详细介绍此时service已经被

关闭

点击unbindservice

基于Android Service 生命周期的详细介绍

先点击startservice,再依次点击bindservice和unbindservice

基于Android Service 生命周期的详细介绍

< body="">

大佬总结

以上是大佬教程为你收集整理的基于Android Service 生命周期的详细介绍全部内容,希望文章能够帮你解决基于Android Service 生命周期的详细介绍所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。