Android开发之Server

xiaoxiao2021-02-28  116

服务的创建与启动:

首先需要创建一个继承于Service的类,然后在AndroidManifest.xml中注册为服务。

接下来需要重写Service类中的三个方法onCreate,onStartComand和onDestroy。

最后通过调用startService方法传入Intent实例参数创建和启动服务。

此时onCreate和onStartComand方法会被调用。

活动操作服务:

首先需要创建一个继承于Binder的类,然后创建这个类实例,以及在onBind方法中返回这个实例。

接下来需要在活动中创建一个onServiceConnected的匿名类,重写里面的onServiceConnected和onServiceDisconnected方法。

最后通过调用binService方法绑定活动与服务,该方法有三个参数

第一个参数为Intent实例

第二个参数为onServiceConnected类

第三个参数为标志位,如“BIND_AUTO_CREATE”就是绑定服务后自动创建服务,即调用onCreate方法

解除绑定为unbindService方法,该方法只需要传入一个onServiceConnected类参数。

服务类

import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.support.annotation.Nullable; import android.util.Log; /** * Created by Administrator on 2017/11/11. */ public class MyService extends Service{ private DownloadBinder downloadBinder=new DownloadBinder(); /**活动控制服务示例*/ class DownloadBinder extends Binder { public void fun(){ Log.d("MyService","fun"); } } /**将服务与活动绑定时调用*/ @Nullable @Override public IBinder onBind(Intent intent) { return downloadBinder; } /**服务被创建时调用*/ @Override public void onCreate() { super.onCreate(); Log.d("MyService","onCreate"); } /**服务被启动时调用*/ @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d("MyService","onStartCommand"); return super.onStartCommand(intent, flags, startId); } /**服务被停止时调用*/ @Override public void onDestroy() { super.onDestroy(); Log.d("MyService","onDestroy"); } }

活动类

private MyService.DownloadBinder downloadBinder; private ServiceConnection connection=new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { downloadBinder=(MyService.DownloadBinder)iBinder; downloadBinder.fun(); } @Override public void onServiceDisconnected(ComponentName componentName) { } }; Intent intent=new Intent(MainActivity.this,MyService.class); startService(intent); //stopService(intent); bindService(intent,connection,BIND_AUTO_CREATE); //unbindService(connection);

<service android:name=".MyService" android:enabled="true" android:exported="true"> </service>

服务不同于线程,Local Service位于主进程的main线程中,Remove Service位于独立进程的main线程中,每一个activity都能与服务建立联系,而线程独立于activity,当你在某个activity启动了这个线程,但又finish了这个activity,那该线程仍然会执行下去,而其它activity无法控制该线程。

服务的生存周期:

开启服务,onCreate,onStartCommond

绑定服务:onBind

解绑服务:unBind

停止服务:onDestroy

注意:

如果没有开启服务而直接绑定服务,若bindService方法的flag参数为:BIND_AUTO_CREATE,则仍然会创建一个服务,但onStartCommond方法不会得到执行,此时若要销毁服务,则需要解绑服务,而不是停止服务。

如果开启了服务,也绑定了服务,那么想销毁该服务,既需要解除绑定也需要停止服务,即stopService和unBind方法均要执行,先后均可。

若需要将服务设置成前台服务即让其显示在通知栏,可加入以下代码:

Intent intent=new Intent(this,MainActivity.class); PendingIntent pi=PendingIntent.getActivity(this,0,intent,0); Notification notification=new Notification.Builder(this) .setContentTitle("test...") .setContentText("test...") .setWhen(System.currentTimeMillis()) .setSmallIcon(R.drawable.ic_launcher_background) .setContentIntent(pi) .build(); startForeground(1,notification);

转载请注明原文地址: https://www.6miu.com/read-48336.html

最新回复(0)