android控件22 Service

来源:互联网 发布:开源大数据调度系统 编辑:程序博客网 时间:2024/04/30 02:49

1

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >   <Button android:id="@+id/button1"       android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="bind"       />   <Button android:id="@+id/button2"       android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="unbind"       />   <Button android:id="@+id/button3"       android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="start"       />    <Button android:id="@+id/button4"       android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="stop"       /></LinearLayout>

2)com.sxt.MusicService.java

package com.sxt;import android.app.Service;import android.content.Intent;import android.media.MediaPlayer;import android.os.IBinder;import android.util.Log;public class MusicService extends Service {MediaPlayer mplayer = null;//当创建服务时候@Overridepublic void onCreate() {Log.i("sxt", "create");super.onCreate();}//当绑定服务时@Overridepublic IBinder onBind(Intent arg0) {Log.i("sxt", "bind");mplayer = MediaPlayer.create(this.getApplicationContext(), R.raw.kanon);mplayer.setLooping(true);mplayer.start();return null;}//当解除服务时@Overridepublic boolean onUnbind(Intent intent) {Log.i("sxt", "unbind");mplayer.stop();return super.onUnbind(intent);}//当开始服务时/*@Overridepublic void onStart(Intent intent, int startId) {Log.i("sxt", "start");super.onStart(intent, startId);}*/@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {// TODO Auto-generated method stubLog.i("sxt", "startCommand");mplayer = MediaPlayer.create(this.getApplicationContext(), R.raw.kanon);mplayer.setLooping(true);mplayer.start();return super.onStartCommand(intent, flags, startId);}//当销毁服务时@Overridepublic void onDestroy() {Log.i("sxt", "destroy");mplayer.stop();super.onDestroy();}}

3)com.sxt.SerivceActivity.java

package com.sxt;import android.app.Activity;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class ServiceActivity extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);        Button button1 = (Button)this.findViewById(R.id.button1);Button button2 = (Button)this.findViewById(R.id.button2);Button button3 = (Button)this.findViewById(R.id.button3);Button button4 = (Button)this.findViewById(R.id.button4);        //定义服务链接对象,用于绑定服务final ServiceConnection serviceConnection = new ServiceConnection(){@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {// TODO Auto-generated method stub}@Overridepublic void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stub}            };                //定义按键事件       OnClickListener onClickListener = new OnClickListener()       {       @Override       public void onClick(View v) {       // TODO Auto-generated method stub       Intent intent = new  Intent(ServiceActivity.this,MusicService.class);       if(v.getId()==R.id.button1)       {       ServiceActivity.this.bindService(intent,serviceConnection,Context.BIND_AUTO_CREATE);             }       if(v.getId()==R.id.button2)       {       ServiceActivity.this.unbindService(serviceConnection);       }       if(v.getId()==R.id.button3)       {       ServiceActivity.this.startService(intent);       }       if(v.getId()==R.id.button4)      {       ServiceActivity.this.stopService(intent);      }       }           };       button1.setOnClickListener(onClickListener);       button2.setOnClickListener(onClickListener);       button3.setOnClickListener(onClickListener);       button4.setOnClickListener(onClickListener);}}


4)AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.sxt"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk android:minSdkVersion="10" />    <application        android:icon="@drawable/ic_launcher"        android:label="@string/app_name" >        <activity            android:label="@string/app_name"            android:name=".ServiceActivity" >            <intent-filter >                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>                 <service android:name=".MusicService">              <intent-filter>                  <action android:name="com.sxt.MusicService"></action>              </intent-filter>          </service>      </application></manifest>
5)如图