服务的生命周期-绑定服务的生命周期

来源:互联网 发布: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
原创粉丝点击