采用绑定的方式开启服务&调用服务的方法

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="visizen.com.bindservice">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".MyService"></service>
    </application>

</manifest>

activity_main.xml

<?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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/startSrv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="startSrv"
        android:text="开启服务" />

    <Button
        android:id="@+id/stopSrv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="stopSrv"
        android:text="关闭服务" />

    <Button
        android:id="@+id/bindSrv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="bindSrv"
        android:text="绑定服务" />

    <Button
        android:id="@+id/unbindSrv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="释放绑定" />

    <Button
        android:id="@+id/speak"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:nestedScrollingEnabled="false"
        android:onClick="speak"
        android:text="输出一段话" />
</LinearLayout>

MainActivity

package visizen.com.bindservice;

import android.content.ComponentName;
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;

public class MainActivity extends AppCompatActivity {

    //private Button startSrv;
    //private Button stopSrv;

    private MyServiceConnection conn;

    Service s;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //this.startSrv= (Button) findViewById(R.id.startSrv);

        //this.stopSrv= (Button) findViewById(R.id.stopSrv);
    }

    public void startSrv(View view) {
        System.out.println("startSrv");
        Intent intent = new Intent(this, MyService.class);
        startService(intent);
    }

    public void stopSrv(View view) {
        System.out.println("stopSrv");
        Intent intent = new Intent(this, MyService.class);
        this.stopService(intent);
    }

    public void bindSrv(View view) {
        System.out.println("bindSrv");
        Intent intent = new Intent(this, MyService.class);
        conn = new MyServiceConnection();
        bindService(intent, conn, BIND_AUTO_CREATE);
    }

    public void unbindSrv(View view) {
        unbindService(conn);
    }

    public void speak(View view) {
        s.sing("大河向东流呀,你有,我有!");
    }

    private class MyServiceConnection implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {

            s = (Service) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }
}

MyService

package visizen.com.bindservice;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.Toast;

/**
 * Created by Administrator on 2015/12/16 0016.
 */
public class MyService extends Service {
    @Override
    public void onCreate() {
        super.onCreate();
        System.out.println("onCreate--------------------------------------------------------------------------");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        System.out.println("onDestroy");
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        System.out.println("onBind-----------------------------------");
        MyBinder binder=new MyBinder();
        return binder;
    }


    public void singSong(String songName){
        Toast.makeText(getApplicationContext(),"您唱的歌曲是:"+songName,Toast.LENGTH_SHORT).show();
    }
    private class MyBinder extends Binder implements visizen.com.bindservice.Service{

        @Override
        public void sing(String song) {
            singSong(song);
        }
    }


}

Service

package visizen.com.bindservice;

/**
 * Created by Administrator on 2015/12/16 0016.
 */
public interface Service {

    public void sing(String song);
}

 

posted on 2015-12-16 18:04  jayhtt  阅读(118)  评论(0)    收藏  举报