赋料扬雄敌,诗看子建亲。这篇文章主要讲述Android中的Service基础相关的知识,希望能为你提供帮助。
Service主要用于后台程序和跨进程访问,可以在不显示界面的前提下完成任务,不影响用户的其他操作。
这里我展示一些基本的用法
新建一个Service类
package com.example.service; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.widget.Toast; public class MyService extends Service { private IBinder myBinder=new Binder(){ public String getInterfaceDescriptor(){ return "MyServerceUtil class."; }}; public IBinder onBind(Intent intent)//绑定时触发 { Toast.makeText(getApplicationContext(),"服务绑定",Toast.LENGTH_SHORT).show(); return myBinder; } public void onRebind(Intent intent)//重新绑定时触发 { super.onRebind(intent); } public boolean onUnbind(Intent intent)//解除绑定时触发 {Toast.makeText(getApplicationContext(),"服务解绑",Toast.LENGTH_SHORT).show(); return super.onUnbind(intent); }public void onCreate(){ super.onCreate(); }//创建时触发 public void onDestroy(){ super.onDestroy(); Toast.makeText(getApplicationContext(),"服务销毁",Toast.LENGTH_SHORT).show(); }public int onStartCommand(Intent intent,int flags,int startId)//启动时触发 { Toast.makeText(getApplicationContext(),"服务启动",Toast.LENGTH_SHORT).show(); returnService.START_NOT_STICKY; } }
【Android中的Service基础】在Mainactivity中调用
package com.example.service; import androidx.appcompat.app.AppCompatActivity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { private Button start=null; private Button bind=null; private Button stop=null; private Button unbind=null; private ServiceConnection serviceConnection =new ServiceConnection() { @Override //连接 public void onServiceConnected(ComponentName name, IBinder service) {}@Override //断开连接 public void onServiceDisconnected(ComponentName name) {} }; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); start=(Button)super.findViewById(R.id.start); stop=(Button)super.findViewById(R.id.stop); bind=(Button)super.findViewById(R.id.bind); unbind=(Button)super.findViewById(R.id.unbind); start.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { MainActivity.this.startService(new Intent(MainActivity.this,MyService.class)); } }); stop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { MainActivity.this.stopService(new Intent(MainActivity.this,MyService.class)); } }); bind.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //绑定 MainActivity.this.bindService(new Intent(MainActivity.this,MyService.class), MainActivity.this.serviceConnection, Context.BIND_AUTO_CREATE); } }); unbind.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //解除绑定 MainActivity.this.unbindService(MainActivity.this.serviceConnection); } }); } }
服务的onCreate()函数在服务第一次启动时会执行一次,之后若不销毁的话再次启动就不会执行,只执行onStartCommand
绑定的用途是把Activity与Service绑定在一起,绑定后当Activity停止时,Service也会停止;未绑定时Activity退出后,Service会仍然运行。
推荐阅读
- 春节宅家火了短视频,手游 APP 成最大赢家!
- flutter Your app isn't using AndroidX
- Android_DatePickerDialog和TimePickerDialog
- android------口算测试APP
- Android_下方弹出菜单的实现
- Android_内部存储文件的读写
- Android Android.mk
- 进度2_家庭记账本App
- 微信小程序导入Vant-Weapp组件库及出错处理