实例:创建、启动、停止和绑定一个Service
来源:互联网 发布:程序员工作累吗? 编辑:程序博客网 时间:2024/06/15 14:05
创建MainActivity:
import android.app.Activity; import android.app.Service; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.Toast; import layout.MyService; public class MainActivity extends Activity implements View.OnClickListener { private Button btn_start,btn_stop,btn_bind,btn_unbind; private Intent intent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_start = (Button) findViewById(R.id.btn_start); btn_stop = (Button) findViewById(R.id.btn_stop); btn_bind = (Button) findViewById(R.id.btn_bind); btn_unbind = (Button) findViewById(R.id.btn_unbind); intent = new Intent(MainActivity.this, MyService.class); btn_start.setOnClickListener(this); btn_stop.setOnClickListener(this); btn_bind.setOnClickListener(this); btn_unbind.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn_start: startService(intent); break; case R.id.btn_stop: stopService(intent); break; case R.id.btn_bind: bindService(intent, conn, Service.BIND_AUTO_CREATE); break; case R.id.btn_unbind: unbindService(conn); break; } } private ServiceConnection conn = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { Log.i("SERVICE","连接成功"); Toast.makeText(MainActivity.this,"连接成功",Toast.LENGTH_LONG).show(); } @Override public void onServiceDisconnected(ComponentName componentName) { Log.i("SERVICE","断开连接"); Toast.makeText(MainActivity.this,"断开连接",Toast.LENGTH_LONG).show(); } }; }
创建MyService:
import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.util.Log;import android.widget.Toast;public class MyService extends Service { @Override public IBinder onBind(Intent intent) { Log.i("SERVICE","<---------onBind--------->"); Toast.makeText(MyService.this,"<---------onBind--------->",Toast.LENGTH_LONG).show(); return null; //可以返回null,通常返回一个有aidl定义的接口 } // Service创建时调用 @Override public void onCreate() { Log.i("SERVICE","<---------onCreate--------->"); Toast.makeText(MyService.this,"<---------onCreate--------->",Toast.LENGTH_LONG).show(); } //当客户端调用startService()方法启动Service时,该方法被调用 @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i("SERVICE","<---------onStartCommand--------->"); Toast.makeText(MyService.this,"<---------onStartCommand--------->",Toast.LENGTH_LONG).show(); return START_STICKY; } //当Service不再使用时调用 @Override public void onDestroy() { Log.i("SERVICE","<---------onDestroy--------->"); Toast.makeText(MyService.this,"<---------onDestroy--------->",Toast.LENGTH_LONG).show(); }}
layout中activity_main是:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="绑定Service练习" android:textSize="20dp"/> <Button android:text="@string/btn_start" android:id="@+id/btn_start" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:text="@string/btn_stop" android:id="@+id/btn_stop" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:text="@string/btn_bind" android:id="@+id/btn_bind" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:text="@string/btn_unbind" android:id="@+id/btn_unbind" android:layout_width="match_parent" android:layout_height="wrap_content" /></LinearLayout>
结果如图:
0 0
- 实例:创建、启动、停止和绑定一个Service
- 启动和停止Service
- 启动和停止Service
- 创建、配置、启动、停止Service
- 初学Android,创建,启动,停止Service(五十八)
- 初学Android,创建,启动,停止Service(五十八)
- Service 启动和绑定流程
- android中Service的启动,停止,绑定的注意事项
- 在installshield中如何创建service and 启动 停止 service
- 服务(Service)的停止和启动
- mysql 多实例启动和停止脚本
- 设置自动启动和停止oracle实例
- 安卓:service,启动service和绑定service生命周期
- Service:启动模式和绑定模式
- Activity与Service的启动和绑定
- android Service 基本创建调用和绑定
- HTML5画布启动和停止一个动画
- Android Service: 启动service, 停止service
- Mybatis深入了解(五)----动态SQL
- Codeforces 689D Friends and Subsequences (RMQ+二分)
- 退出后点击浏览器后退不能回到成功页面;直接进入页面和跳转进入页面样式不同;防止直接输入页面地址或servlet;当servlet跳servlet时,进行其他操作后后退出现错误!
- android中wifi原理及流程分析(很经典)
- 【DRP】-servlet 请求传值
- 实例:创建、启动、停止和绑定一个Service
- javaweb里面的/是否出现总结
- jQuery 淡入淡出、滑动和动画
- MySQL开启慢查询
- 爬取微信公众号
- 【Thinking in Java真题精选】1. 类的初始化顺序
- 继承的简单理解,原型对象继承,类继承,混合方式继承
- Win32游戏制作之---五子棋的简单设计
- 腾讯2017实习生编程之算法基础-字符移位