旺仔练习:service的普通方式和绑定方式的启动和停止(解绑出现异常,尚未解决)
package com.hanqi.dianhuabohaoqi; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.IBinder; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class usingservice extends AppCompatActivity { Button bt_1,bt_2,bt_3,bt_4; Intent intentservice; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_usingservice); bt_1=(Button)findViewById(R.id.bt_1); bt_2=(Button)findViewById(R.id.bt_2); bt_3=(Button)findViewById(R.id.bt_3); bt_4=(Button)findViewById(R.id.bt_4); bt_1.setOnClickListener(new start_service()); bt_2.setOnClickListener(new start_service()); bt_3.setOnClickListener(new start_service()); bt_4.setOnClickListener(new start_service()); intentservice=new Intent(this,EchoService.class); } class start_service implements View.OnClickListener { ServiceConnection sc; public void onClick(View v) { if (sc==null){ sc= new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { //接收代理对象进行强转 EchoService.MyBind echo=(EchoService.MyBind)service; Toast.makeText(usingservice.this, "绑定service成功连接,接收返回对象="+echo.gettest(), Toast.LENGTH_SHORT).show(); } @Override public void onServiceDisconnected(ComponentName name) { } }; } switch (v.getId()) { case R.id.bt_1://启动service startService(intentservice); break; case R.id.bt_2://停止service stopService(intentservice); break; case R.id.bt_3://绑定方式启动service bindService(intentservice,sc , Context.BIND_AUTO_CREATE); break; case R.id.bt_4://解除绑定 if (sc!=null){ unbindService(sc); sc=null; }else { Toast.makeText(usingservice.this, "清先绑定服务", Toast.LENGTH_SHORT).show(); } break; } } } }
package com.hanqi.dianhuabohaoqi; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.util.Log; public class EchoService extends Service { public EchoService() { Log.e("TAG","service被构造"); } @Override public void onCreate() { Log.e("TAG","启动service"); super.onCreate(); } @Override public void onDestroy() { Log.e("TAG","停止service"); super.onDestroy(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.e("TAG","onStartCommand被调用"); return super.onStartCommand(intent, flags, startId); } @Override public boolean onUnbind(Intent intent) { Log.e("TAG","onUnbind被调用"); return super.onUnbind(intent); } //内部类 public class MyBind extends Binder { //定义数据交互的方法 public int gettest() { return 123; } }; @Override public IBinder onBind(Intent intent) { Log.e("TAG","onBind被调用"); // throw new UnsupportedOperationException("Not yet implemented"); return new MyBind(); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.hanqi.dianhuabohaoqi.usingservice"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/bt_1" android:text="启动service"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/bt_2" android:text="停止service"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/bt_3" android:text="绑定service"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/bt_4" android:text="解除绑定service"/> </LinearLayout>