欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 幼教 > 【安卓】Service的基本用法

【安卓】Service的基本用法

2024/10/24 19:25:11 来源:https://blog.csdn.net/qq_47658735/article/details/141196699  浏览:    关键词:【安卓】Service的基本用法

文章目录

      • Service简介
      • 启动和停止Service
      • Activity和Service进行通信

Service简介

  新建一个ServiceTest项目,然后右击com.example.servicetest→New→Service→Service。

  每个Service中最常用到onCreate()、onStartCommand()和onDestroy()这3个方法其中onCreate()方法会在Service创建的时候调用,onStartCommand()方法会在每次Service启动的时候调用,onDestroy()方法会在Service销毁的时候调用。

  另外需要注意,每一个Service都需要在AndroidManifest.xml文件中进行注册才能生效。

启动和停止Service

  修改activity_main.xml中的代码。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><Buttonandroid:id="@+id/startServiceBtn"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Start Service" /><Buttonandroid:id="@+id/stopServiceBtn"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Stop Service" />
</LinearLayout>

  修改MainActivity中的代码。

public class MainActivity extends AppCompatActivity implements View.OnClickListener {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button startService = (Button) findViewById(R.id.startServiceBtn);Button stopService = (Button) findViewById(R.id.stopServiceBtn);startService.setOnClickListener(this);stopService.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.startServiceBtn:Intent startIntent = new Intent(this, MyService.class);startService(startIntent); // 启动服务break;case R.id.stopServiceBtn:Intent stopIntent = new Intent(this, MyService.class);stopService(stopIntent); // 停止服务break;default:break;}}
}

  修改MyService.java中的代码。

public class MyService extends Service {public MyService() {}@Overridepublic IBinder onBind(Intent intent) {// TODO: Return the communication channel to the service.throw new UnsupportedOperationException("Not yet implemented");}@Overridepublic void onCreate(){super.onCreate();Log.d("MyService", "onCreate executed");}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.d("MyService", "onStartCommand executed");return super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() {super.onDestroy();Log.d("MyService", "onDestroy executed");}
}

Activity和Service进行通信

  修改MyService中的代码。

public class MyService extends Service {private DownloadBinder mBinder = new DownloadBinder();class DownloadBinder extends Binder {public void startDownload() {Log.d("MyService", "startDownload executed");}public int getProgress() {Log.d("MyService", "getProgress executed");return 0;}}public MyService() {}@Overridepublic IBinder onBind(Intent intent) {return mBinder;}@Overridepublic void onCreate(){super.onCreate();Log.d("MyService", "onCreate executed");}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.d("MyService", "onStartCommand executed");return super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() {super.onDestroy();Log.d("MyService", "onDestroy executed");}
}

  修改activity_main.xml中的代码,在布局文件里新增两个按钮用于调用Service。这两个按钮分别是用于绑定和取消绑定Service的,当一个Activity和Service绑定了之后,就可以调用该Service里的Binder提供的方法了。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><Buttonandroid:id="@+id/startServiceBtn"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Start Service" /><Buttonandroid:id="@+id/stopServiceBtn"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Stop Service" /><Buttonandroid:id="@+id/bindServiceBtn"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Bind Service" /><Buttonandroid:id="@+id/unbindServiceBtn"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Unbind Service" />
</LinearLayout>

  修改MainActivity中的代码。

public class MainActivity extends AppCompatActivity implements View.OnClickListener {private MyService.DownloadBinder downloadBinder;private ServiceConnection connection = new ServiceConnection() {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {downloadBinder = (MyService.DownloadBinder) service;downloadBinder.startDownload();downloadBinder.getProgress();}@Overridepublic void onServiceDisconnected(ComponentName name) {}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button startService = (Button) findViewById(R.id.startServiceBtn);Button stopService = (Button) findViewById(R.id.stopServiceBtn);startService.setOnClickListener(this);stopService.setOnClickListener(this);Button bindService = (Button) findViewById(R.id.bindServiceBtn);Button unbindService = (Button) findViewById(R.id.unbindServiceBtn);bindService.setOnClickListener(this);unbindService.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.startServiceBtn:Intent startIntent = new Intent(this, MyService.class);startService(startIntent); // 启动服务break;case R.id.stopServiceBtn:Intent stopIntent = new Intent(this, MyService.class);stopService(stopIntent); // 停止服务break;case R.id.bindServiceBtn:Intent bindIntent = new Intent(this, MyService.class);bindService(bindIntent, connection, BIND_AUTO_CREATE); // 绑定服务break;case R.id.unbindServiceBtn:unbindService(connection);  // 解绑服务break;default:break;}}
}

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com