Service

本文介绍Android中服务的两种启动方式:Start方式和服务跟启动源无关联;Bind方式则通过Ibinder接口实例返回ServiceConnection对象给启动源,并可通过该对象获取Service对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Start方式的特点:

服务跟启动源没有任何联系

无法的带服务对象

Bind方式的特点:

通过Ibinder接口实例,返回一个ServiceConnection对象给启动源

通过ServiceConnection对象的相关方法可以得到Service对象

package com.example.servicedemo;

import com.example.servicedemo.MyBindService.MyBinder;

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.view.Menu;
import android.view.MenuItem;
import android.view.View;


public class MainActivity extends Activity {
	Intent intent1;
	Intent intent2;
	MyBindService service;
	ServiceConnection conn=new ServiceConnection() {
		//当启动源跟Service的链接意外丢失的时候会调用这个方法
		//比如当Service崩溃了或被强行杀死了
		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
			
		}
		//当启动源跟Service连接成功之后会自动调用这个方法
		@Override
		public void onServiceConnected(ComponentName name, IBinder binder) {
			// TODO Auto-generated method stub
			service=((MyBinder)binder).getService();
		}
	};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
public void doClick(View view){
	switch (view.getId()) {
	case R.id.start:
	    intent1=new Intent(MainActivity.this,MySyartService.class);
		startService(intent1);
		break;
    case R.id.stop:
		stopService(intent1);
		break;
    case R.id.bind:
		intent2=new Intent(MainActivity.this,MyBindService.class);
		bindService(intent2,conn,Service.BIND_AUTO_CREATE);
		break;
    case R.id.unbind:
		unbindService(conn);
		break;
		case R.id.play:
			service.play();
			break;
    case R.id.next:
			service.next();
			break;
    case R.id.push:
	service.push();
	     break;
    case R.id.pervious:
	service.pervious();
	     break;
	default:
		break;
	}
}
}

package com.example.servicedemo;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class MySyartService extends Service {
@Override
public void onCreate() {
	// TODO Auto-generated method stub
	super.onCreate();
	Log.i("Info","service--onCreate()");
}
@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
	    Log.i("Info","service--onStartCommand()");
		return super.onStartCommand(intent, flags, startId);
		
	}
@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.i("Info","service--onDestroy()");
	}
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		Log.i("Info","service--onBind()");
		return null;
	}

}

package com.example.servicedemo;

import android.app.Service;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class MyBindService extends Service{
@Override
public void onCreate() {
	// TODO Auto-generated method stub
	super.onCreate();
	Log.i("Info","BindService-onCreate()");
}
public class MyBinder extends Binder{
	public MyBindService getService(){
		return MyBindService.this;
	}
}
@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.i("Info","BindService-onDestroy()");
	}
@Override
	public void unbindService(ServiceConnection conn) {
		// TODO Auto-generated method stub
		super.unbindService(conn);
		Log.i("Info","BindService-unbindService()");
	}
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		Log.i("Info","BindService-onBind()");
		return new MyBinder();
	}
	public void play(){
		Log.i("Infor","播放");
	}
	public void pervious(){
		Log.i("Infor","上一首");
	}
	public void push(){
		Log.i("Infor","暂停");
	}
	public void next(){
		Log.i("Infor","下一首");
	}

}

<RelativeLayout 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: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="com.example.servicedemo.MainActivity" >

    <Button
        android:id="@+id/pervious"
        android:onClick="doClick"
        android:layout_width="fill_parent"
        android:layout_height="30dp"
        android:textSize="10dp"
        android:layout_alignLeft="@+id/push"
        android:layout_below="@+id/next"
        android:text="上一首" />

    <Button
        android:id="@+id/next"
        android:onClick="doClick"
        android:layout_width="fill_parent"
        android:layout_height="30dp"
        android:textSize="10dp"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/push"
        android:text="下一首" />

    <Button
        android:id="@+id/unbind"
        android:onClick="doClick"
        android:layout_width="fill_parent"
        android:layout_height="30dp"
        android:textSize="10dp"
        android:layout_alignLeft="@+id/pervious"
        android:layout_below="@+id/pervious"
        android:text="UnBindService" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="Start:"
        android:textSize="20dp" />

    <Button
        android:id="@+id/start"
        android:layout_width="fill_parent"
        android:layout_height="30dp"
        android:textSize="10dp"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:onClick="doClick"
        android:text="startService" />

    <Button
        android:id="@+id/stop"
        android:layout_width="fill_parent"
        android:layout_height="30dp"
        android:textSize="10dp"
        android:layout_alignLeft="@+id/start"
        android:layout_below="@+id/start"
        android:onClick="doClick"
        android:text="StopService" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView1"
        android:layout_below="@+id/stop"
        android:text="Bind:"
        android:textSize="20dp" />

    <Button
        android:id="@+id/bind"
        android:layout_width="fill_parent"
        android:layout_height="30dp"
        android:textSize="10dp"
        android:layout_alignLeft="@+id/stop"
        android:layout_below="@+id/textView2"
        android:onClick="doClick"
        android:text="BindService" />

    <Button
        android:id="@+id/push"
        android:layout_width="fill_parent"
        android:layout_height="30dp"
        android:textSize="10dp"
        android:layout_alignLeft="@+id/play"
        android:layout_below="@+id/play"
        android:onClick="doClick"
        android:text="暂停" />

    <Button
        android:id="@+id/play"
        android:layout_width="fill_parent"
        android:layout_height="30dp"
        android:layout_alignRight="@+id/bind"
        android:layout_below="@+id/bind"
        android:onClick="doClick"
        android:text="播放"
        android:textSize="10dp" />
    
</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.servicedemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="20" />

    <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.example.servicedemo.MySyartService"
            ></service>
        <service 
            android:name="com.example.servicedemo.MyBindService"
            ></service>
    </application>

</manifest>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值