旺仔练习:service的普通方式和绑定方式的启动和停止(解绑出现异常,尚未解决)
2016-07-16 12:25
330 查看
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>
相关文章推荐
- static和volatile的用法
- 【漫画解读】HDFS存储原理
- c++中定义结构体类型变量的方法
- 香蕉派 banana pi BPI-M64 四核心64位开源单板计算机 全志 allwinner A64方案
- css+js实现图片反转效果
- 移动端1px边框实现
- the right way to select table rows programmingly
- 四位可逆素数
- win7开机运行脚本程序的方法
- 孪生素数
- java启动操作系统上安装的应用程序
- Intellij idea log4j 控制台console输出乱码问题的解决办法
- 某高人整理的Java就业面试题大全【1】
- linux 为文件添加、减少权限
- 视频有关“技术调研”
- MySQL配置
- 18 树的子结构
- 回文素数
- Web.py - SSL签名
- 约瑟夫问题