android service
Service基本用法
新建一个MyService类集成Service,重写Service的onCreate()、onStartCommand()和onDestroy()函数。
public class MyService extends Service { private final static String TAG = "MyService"; @Override public void onCreate() { super.onCreate(); Log.d(TAG, "onCreate"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(TAG, "onStartCommand"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy"); } @Nullable @Override public IBinder onBind(Intent intent) { return null; } }
在AndroidManifest.xml中注册service
<service android:name=".MyService"></service>
在activity_main.xml中添加两个button用于启动和停止service
<Button android:id="@+id/start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="80dp" android:layout_marginTop="148dp" android:text="start" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="220dp" android:layout_marginTop="72dp" android:text="stop" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
编辑activity添加service启动停止逻辑。使用Context.startService()启动服务,使用Context.stopService()停止服务
Button btnStart=(Button)findViewById(R.id.start); btnStart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(v.getContext(),MyService.class); startService(intent); } }); Button btnStop=(Button)findViewById(R.id.stop); btnStop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(v.getContext(),MyService.class); stopService(intent); } });
Service和Activity绑定
修改onBind函数。添加MyBinder类
private final static String TAG = "MyService"; private MyBinder myBinder = new MyBinder(); @Override public void onCreate() { super.onCreate(); Log.d(TAG, "onCreate"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(TAG, "onStartCommand"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy"); } @Nullable @Override public IBinder onBind(Intent intent) { return myBinder; } class MyBinder extends Binder { public void startBinder(){ Log.d(TAG,"startBinder"); } }
修改activity_main.xml文件添加绑定和移除绑定按钮
<Button android:id="@+id/start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="80dp" android:layout_marginTop="148dp" android:text="start" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="220dp" android:layout_marginTop="72dp" android:text="stop" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/binder" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:text="binder" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/unBinder" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="124dp" android:layout_marginTop="16dp" android:text="unBinder" app:layout_constraintStart_toStartOf="@+id/binder" app:layout_constraintTop_toTopOf="parent" />
添加绑定和移除绑定按钮事件
ServiceConnection serviceConnection=new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { MyService.MyBinder myBinder=(MyService.MyBinder)service; myBinder.startBinder(); } @Override public void onServiceDisconnected(ComponentName name) { } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btnStart=(Button)findViewById(R.id.start); btnStart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(v.getContext(),MyService.class); startService(intent); } }); Button btnStop=(Button)findViewById(R.id.stop); btnStop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(v.getContext(),MyService.class); stopService(intent); } }); Button btnBinder=(Button)findViewById(R.id.binder); btnBinder.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(v.getContext(),MyService.class); bindService(intent,serviceConnection,BIND_AUTO_CREATE); } }); Button btnUnBinder=(Button)findViewById(R.id.unBinder); btnUnBinder.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { unbindService(serviceConnection); } }); }
当服务绑定activity后,必须移除绑定并停止服务以后服务才会被真正销毁