Service是后台运行,不可见,没有界面的页面,优先级高于Activity,可以用来播放音乐、记录地理信息位置的改变、监听某种动作,类型有两种,一是本地服务,有start和bind两种启动方式,另一种是远程服务。
目标效果:
程序运行显示所有的按钮控件,分为两类,上边是start的启动和停止,下边是bind的启动和停止,点击输出对应的生命周期的方法内容。
1.activity_main.xml页面放置所有按钮控件。
activity_main.xml页面:
<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" tools:context=".MainActivity" > <TextView android:id="@+id/tvStart" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="Start:" /> <Button android:id="@+id/btStart" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_below="@+id/tvStart" android:text="StartService" /> <Button android:id="@+id/btStop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_below="@+id/btStart" android:text="StopService" /> <TextView android:id="@+id/tvBind" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/btStop" android:text="Bind:" /> <Button android:id="@+id/btBind" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_below="@+id/tvBind" android:text="BindService" /> <LinearLayout android:id="@+id/changeOne" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/btBind" android:orientation="horizontal" > <Button android:id="@+id/btPrevious" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_weight="1" android:text="上一首" /> <Button android:id="@+id/btNext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_weight="1" android:text="下一首" /> </LinearLayout> <LinearLayout android:id="@+id/changeTwo" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/changeOne" android:orientation="horizontal" > <Button android:id="@+id/btPlay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_weight="1" android:text="播放" /> <Button android:id="@+id/btPause" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_weight="1" android:text="暂停" /> </LinearLayout> <Button android:id="@+id/btUnBind" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_below="@+id/changeTwo" android:text="UnBindService" /> </RelativeLayout>
2.新建myStartService.java页面继承Service,重写方法start方式启动时调用。
myStartService.java页面:
package com.example.service; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; public class myStartService extends Service{ @Override public IBinder onBind(Intent arg0) { Log.i("MainActivity","startService--onBind()"); return null; } @Override public void onCreate() { Log.i("MainActivity","startService--onCeate()"); super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i("MainActivity","startService--onStartCommand()"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); Log.i("MainActivity","startService--onDestroy()"); } }
3.新建myBindService.java页面继承Service,重写方法,bind方式启动时调用。
myBindService.java页面:
package com.example.service; 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{ /*定义类继承Binder返回服务对象*/ public class MyBinder extends Binder{ public myBindService getService(){ return myBindService.this; } } @Override public IBinder onBind(Intent arg0) { Log.i("MainActivity","bindService--onBind()"); return new MyBinder();//实例服务对象并返回 } @Override public void onCreate() { super.onCreate(); Log.i("MainActivity","bindService--onCeate()"); } @Override public void unbindService(ServiceConnection conn) { super.unbindService(conn); Log.i("MainActivity","bindService--unbindService()"); } @Override public void onDestroy() { super.onDestroy(); Log.i("MainActivity","bindService--onDestroy()"); } public void play(){ Log.i("MainActivity","播放"); } public void pause(){ Log.i("MainActivity","暂停"); } public void previous(){ Log.i("MainActivity","上一首"); } public void next(){ Log.i("MainActivity","下一首"); } }
4.对于新建的两个Service,都需要在AndroidManifest.xml页面进行注册。
AndroidManifest.xml页面:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.service" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.service.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 --> <service android:name="com.example.service.myStartService"></service> <service android:name="com.example.service.myBindService"></service> </application> </manifest>
5.MainActivity.java页面进行点击事件的绑定。
MainActivity.java页面:
package com.example.service; import com.example.service.myBindService.MyBinder; import android.os.Bundle; import android.os.IBinder; import android.app.Activity; import android.app.Service; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity implements OnClickListener { private Button btStart, btStop, btBind, btPrevious, btNext, btPlay, btPause, btUnBind; private Intent intentStart,intentBind; private myBindService service; /*得到服务对象*/ private ServiceConnection conn=new ServiceConnection() { /*当启动源跟Service的连接意外丢失的时候会调用这个方法,比如当Service崩溃了或者被强行杀死了*/ @Override public void onServiceDisconnected(ComponentName arg0) { } /*当启动源跟Service成功连接之后将会自动调用这个方法*/ @Override public void onServiceConnected(ComponentName name, IBinder binder) { service=((MyBinder)binder).getService();//得到服务对象 } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getId(); bindClick(); } private void getId() { btStart = (Button) findViewById(R.id.btStart); btStop = (Button) findViewById(R.id.btStop); btBind = (Button) findViewById(R.id.btBind); btPrevious = (Button) findViewById(R.id.btPrevious); btNext = (Button) findViewById(R.id.btNext); btPlay = (Button) findViewById(R.id.btPlay); btPause = (Button) findViewById(R.id.btPause); btUnBind = (Button) findViewById(R.id.btUnBind); } private void bindClick() { btStart.setOnClickListener(this); btStop.setOnClickListener(this); btBind.setOnClickListener(this); btPrevious.setOnClickListener(this); btNext.setOnClickListener(this); btPlay.setOnClickListener(this); btPause.setOnClickListener(this); btUnBind.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.btStart: intentStart=new Intent(MainActivity.this,myStartService.class); startService(intentStart); break; case R.id.btStop: stopService(intentStart); break; case R.id.btPlay: service.play(); break; case R.id.btPause: service.pause(); break; case R.id.btPrevious: service.previous(); break; case R.id.btNext: service.next(); break; case R.id.btBind://绑定 intentBind=new Intent(MainActivity.this,myBindService.class); //第一个参数为实例的intent对象,第二个参数为得到的服务对象 bindService(intentBind, conn,Service.BIND_AUTO_CREATE); break; case R.id.btUnBind://解绑,只可点击一次,并且如果想要结束启动源,必须解绑 unbindService(conn); break; } } }
6.运行就显示目标效果了。
时间: 2024-10-16 06:06:29