服务的生命周期-绑定服务的生命周期
来源:互联网 发布:finale打谱软件汉化版 编辑:程序博客网 时间:2024/05/01 01:32
<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" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="bind" android:text="绑定服务"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="unbind" android:text="解除服务绑定"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="call" android:text="调用服务里面的方法"/> </LinearLayout>
package com.sql.bindservicelief; import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.view.Menu;import android.view.MenuItem;import android.view.View;public class MainActivity extends Activity {private MyConn conn;private IMiddlePerson mp ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //绑定服务 public void bind(View v){ Intent intent = new Intent(this,MyService.class); /** * bindService参数 * 1.写出你要跳转的服务 * 2.接收服务连接成功和失去连接的方法 * 3.BIND_AUTO_CREATE 如果服务不存在就创建,存在就不创建了 * */ conn = new MyConn(); bindService(intent, conn, Activity.BIND_AUTO_CREATE); } //解除绑定服务 public void unbind(View v){ unbindService(conn); mp = null; //不能再调用服务中的方法 } public void call(View v){ //5.通过中间人调用服务里面的方法。 if(mp != null){ mp.callMethodInService(55); } } @Override protected void onDestroy() { unbindService(conn); super.onDestroy(); } private class MyConn implements ServiceConnection{ ////4. 当服务被连接的时候调用 服务别成功 绑定的时候调用@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {System.out.println("在activity里面成功得到了中间人");mp = (IMiddlePerson) service;}//当服务失去连接的时候调用(一般进程挂了,服务被异常杀死)@Overridepublic void onServiceDisconnected(ComponentName name) {} }}
package com.sql.bindservicelief;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.os.MessageQueue.IdleHandler;import android.widget.Toast;public class MyService extends Service {//2.实现服务成功绑定的代码 ,返回一个中间人。@Overridepublic IBinder onBind(Intent intent) {return new MiddlePerson();}@Overridepublic boolean onUnbind(Intent intent) {return super.onUnbind(intent);}@Overridepublic void onCreate() {System.out.println("oncreate");super.onCreate();}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {System.out.println("onstart");return super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() {System.out.println("ondestroy");super.onDestroy();}/** * 这是服务里面的一个方法 */public void methodInService(){Toast.makeText(this, "哈哈,服务给你办好了暂住证。", 0).show();}//1.第一步服务要暴露方法 必须要有一个中间人private class MiddlePerson extends Binder implements IMiddlePerson{//写成私有的目的是为了不让其他的public void callMethodInService(int money){if(money >= 50){methodInService();}else{Toast.makeText(getApplicationContext(), "多准备点钱。", 0).show();}}/** * 陪领导打麻将 */public void playMajiang(){System.out.println("陪领导打麻将。");} }}
package com.sql.bindservicelief;/** * 中间人的接 * * */public interface IMiddlePerson {//暴露接口的目的就是让调用者只能访问这一个方法,不能访问MyService中的内部类的其他方法/** * 代办暂住证 * */public void callMethodInService(int money);}
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.sql.bindservicelief" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name="com.sql.bindservicelief.MyService"></service> </application></manifest>
0 0
- 服务的生命周期-绑定服务的生命周期
- Android 绑定类型服务---管理绑定类型服务的生命周期
- Android 绑定类型服务---管理绑定类型服务的生命周期 .
- 服务的生命周期
- Android 服务的生命周期
- 服务的生命周期
- Android服务的生命周期
- 输入法服务的生命周期
- 服务的生命周期
- 服务的生命周期
- 服务的生命周期
- Android服务的生命周期
- 服务的生命周期
- 服务的生命周期
- 服务的生命周期
- 服务的生命周期
- 服务的生命周期基础
- service服务的生命周期
- kube 之apiServer学习
- android程序内多语言切换不需要重新启动的解决方案
- Create and Use a Custom Formatter in the Apache HBase Shell
- Mysql 多表查询详解
- 如何解决click事件重复的问题?
- 服务的生命周期-绑定服务的生命周期
- android 调用 su 指令
- U-Boot 使用
- java IO流操作文章
- 使用 CXF 做 webservice 简单例子
- osmdroid中为何显示空地图
- img的宽高无法根据父a元素变化-----内联元素化为块级元素
- 设计模式之单例详解
- 发消息的基类