当前位置: 首页 > news >正文

qq登陆 wordpress清远网站seo

qq登陆 wordpress,清远网站seo,怎样做加入购物车的网站,邯郸注册公司目录 一,Service简介 二,Service的两种启动方式 1,非绑定式启动Service 2,绑定式启动Service 三,Service的生命周期 1,非绑定式Service的生命周期 2,绑定式Service的生命周期 四&#xf…

目录

一,Service简介

 二,Service的两种启动方式

1,非绑定式启动Service

2,绑定式启动Service

三,Service的生命周期

1,非绑定式Service的生命周期

2,绑定式Service的生命周期

四,前台Service

1,前台Service的创建

2,前台Service的结束


一,Service简介

        Service服务,是指执行指定系统功能的程序,例程或进程,以便支持其他程序,并且运行期间用户不可见的一种活动机制,例如:后台播放音乐,后台下载等;

        Service和Activity同属于一个级别,不同于子线程,service是运行在主线程中的,因此不能进行耗时操作;

 二,Service的两种启动方式

(1)非绑定式启动(startService):

  • 服务开启后与启动者没有任何关系,service的生命周期独立于启动者,启动者退出,service仍会运行;
  • 启动者无法调用service中的方法;

(2)绑定式启动(bindService)

  • 启动者(Activity)会和service绑定在一起,两者的生命周期会同步,当启动者退出时,service会跟着被销毁;
  • 启动者可以调用service中的方法;

1,非绑定式启动Service

(1) 创建一个类继承Service类,并重写一系列方法:

public class MyService extends Service {@Nullable@Overridepublic IBinder onBind(Intent intent) {Log.i("MyService", "onBind: ");return null;}@Overridepublic void onCreate() {Log.i("MyService", "onCreate: ");super.onCreate();}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.i("MyService", "onStartCommand: ");return super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() {Log.i("MyService", "onDestroy: ");super.onDestroy();}
}

(2)在Manifest文件中注册指定Service:

(3)调用startService(Intent intent)方法启动Service:

private void startMyService() {Intent intent = new Intent(this, MyService.class);startService(intent);
}

2,绑定式启动Service

(1)前两步与非绑定式启动一致,创建Service子类并注册Service:

public class MyBindService extends Service {private final String TAG = "MyBindService";@Nullable@Overridepublic IBinder onBind(Intent intent) {Log.i(TAG, "onBind: ");return new MyBinder(this);}@Overridepublic void onCreate() {Log.i(TAG, "onCreate: ");super.onCreate();}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.i(TAG, "onStartCommand: ");return super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() {Log.i(TAG, "onDestroy: ");super.onDestroy();}@Overridepublic boolean onUnbind(Intent intent) {Log.i(TAG, "onUnbind: ");return super.onUnbind(intent);}
}

(2)绑定式启动Service需要调用bindService()方法,这个方法需要三个参数:

private void startBindService() {Intent intent = new Intent(this, MyBindService.class);isBound = bindService(intent, connection, BIND_AUTO_CREATE);
}

Intent:表示启动意图,也就是想要启动的Service;

connection:相当于启动者(Activity)和Service之间的连接,通过一系列的回调函数来监听访问者和Service的连接情况;

int flag:绑定时是否自动创建Service,这里选择自动创建BIND_AUTO_CREATE;

        除了Intent和flag外,我们还需创建一个connection,这里通过匿名内部类的形式创建,并重写两个回调方法。这里onServiceConnected方法中有一个IBinder类型的service,这个service起到了中间人的作用,通过这个service,启动者(Activity)就可以调用Service中的方法:

private ServiceConnection connection = new ServiceConnection() {//创建连接时回调@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {//这里 IBinder类型的service 就是我们要绑定的那个service//通过这个service,Activity就可以调用MyBindService.MyBinder中的方法}//断开连接时回调@Overridepublic void onServiceDisconnected(ComponentName name) {Log.i(TAG, "onServiceDisconnected: ");}
};

        那么这个service是从哪来的呢?

        在我们创建的Service子类中,我们重写了一个onBind的方法,返回的正好是一个IBinder类型的值,这个返回值也就是会传给上面service的值。

@Override
public IBinder onBind(Intent intent) {Log.i(TAG, "onBind: ");return new MyBinder(this);
}

        所以我们可以在Service子类中创建一个类继承自Binder(Binder实现了IBinder接口),这样Activity通过connection中的service就可以调用MyBinder类中的方法;

        进一步,通过构造方法,我们可以将Service传给MyBinder,这样在MyBinder中就可以调用我们Service中的方法,又因为Activity可以调用MyBinder中的方法,所以我们就实现了Activity调用Service的方法,这也就是为什么绑定式启动Service,启动者(Activity)可以调用Service中的方法;

@Override
public IBinder onBind(Intent intent) {Log.i(TAG, "onBind: ");return new MyBinder(this);
}public void Test(){//Log.i(TAG, "Test: MyBindService的Test方法被调用");
}public class MyBinder extends Binder{private MyBindService myBindService;public MyBinder(){}public MyBinder(MyBindService bindService){this.myBindService = bindService;}public void Test(){//Log.i(TAG, "Test: MyBinder的Test方法被调用");//这样MyBinder就可以调用MyBindService中的方法//MyBinder作为一个中间人 Activity调用MyBinder的方法 -> MyBinder再调用Service的方法myBindService.Test();}
}

绑定式启动Service的全部流程代码:

Activity

public class MainActivity extends AppCompatActivity {private final String TAG = "MainActivity";private Boolean isBound = false;private ActivityMainBinding binding;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);binding = ActivityMainBinding.inflate(getLayoutInflater());setContentView(binding.getRoot());setLinsteners();}private void setLinsteners() {binding.btnStartBindService.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {startBindService();}});}private void startBindService() {Intent intent = new Intent(this, MyBindService.class);isBound = bindService(intent, connection, BIND_AUTO_CREATE);}private MyBindService.MyBinder myBindService;private ServiceConnection connection = new ServiceConnection() {//创建连接时回调@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {//这里 IBinder类型的service 就是我们要绑定的那个service//通过这个service,Activity就可以调用MyBindService.MyBinder中的方法myBindService = (MyBindService.MyBinder)service;myBindService.Test();}//断开连接时回调@Overridepublic void onServiceDisconnected(ComponentName name) {Log.i(TAG, "onServiceDisconnected: ");//Intent intent = new Intent(MainActivity.this, MyBindService.class);//stopService(intent);}};
}

Service

public class MyBindService extends Service {private final String TAG = "MyBindService";@Nullable@Overridepublic IBinder onBind(Intent intent) {Log.i(TAG, "onBind: ");return new MyBinder(this);}public void Test(){//Log.i(TAG, "Test: MyBindService的Test方法被调用");}public class MyBinder extends Binder{private MyBindService myBindService;public MyBinder(){}public MyBinder(MyBindService bindService){this.myBindService = bindService;}public void Test(){//Log.i(TAG, "Test: MyBinder的Test方法被调用");//这样MyBinder就可以调用MyBindService中的方法//MyBinder作为一个中间人 Activity调用MyBinder的方法 -> MyBinder再调用Service的方法myBindService.Test();}}@Overridepublic void onCreate() {Log.i(TAG, "onCreate: ");super.onCreate();}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.i(TAG, "onStartCommand: ");return super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() {Log.i(TAG, "onDestroy: ");super.onDestroy();}@Overridepublic boolean onUnbind(Intent intent) {Log.i(TAG, "onUnbind: ");return super.onUnbind(intent);}}

三,Service的生命周期

1,非绑定式Service的生命周期

启动阶段:启动者(Activity)调用startService

  • onCreate():Service被创建时调用,整个生命周期中只会被调用一次;
  • onStartCommand():每次调用startService时,该方法会被调用,该方法接收Intent参数,从而可以执行一些命令;

结束阶段:启动者调用stopService()方法或Service内部调用stopSelf()方法;

  • onDestroy():Service销毁时调用,与onCreate一样,整个生命周期中只会被调用一次;

2,绑定式Service的生命周期

启动阶段:启动者(Activity)调用bindService

  • onCreate():Service被创建时调用,整个生命周期中只会被调用一次;
  • onBind():在首次绑定时会被调用一次,同样整个生命周期中只会被调用一次;

结束阶段:当启动者销毁或unBindService方法时,启动者会和Service解除绑定,当没有任何绑定者时,Service会被销毁

  • onUnbind():解除绑定时调用,可多次调用;
  • onDestroy():Service销毁时调用,整个生命周期中只会被调用一次;

四,前台Service

        前台Service,即可以与用户进行交互的运行在前台的Service,优先级相比于其他两种运行在后台的Service要高,最常见的应用就是通知栏前台控制音乐播放;

1,前台Service的创建

        在正常的Service中调用startForeground() 方法即可将正常服务提升为前台服务,startForeground()方法需要接收一个通知对象,因为前台Service必须在通知栏中进行通知;

public class MyForeGroundService extends Service {private final String TAG = "MyForeGroundService";@Nullable@Overridepublic IBinder onBind(Intent intent) {Log.i(TAG, "onBind: ");return null;}@Overridepublic void onCreate() {super.onCreate();Log.i(TAG, "onCreate: ");//创建一个通知NotificationManager notificationManager = (NotificationManager) getApplication().getSystemService(Context.NOTIFICATION_SERVICE);NotificationChannel channel = new NotificationChannel("channel_id","channel_name",notificationManager.IMPORTANCE_HIGH);notificationManager.createNotificationChannel(channel);Notification.Builder builder = new Notification.Builder(this,"channel_id");Notification notification = builder.build();//将服务提升为前台服务startForeground(1, notification);}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.i(TAG, "onStartCommand: ");return super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() {Log.i(TAG, "onDestroy: ");super.onDestroy();}}

2,前台Service的结束

 前台Service的结束有两种含义:

(1)结束Service本身:通过启动者调用stopService方法或Service内部调用stopSelf方法正常结束Service,Service结束后,通知也会随之移除;

(2)前台Service降级为后台Service:通过Service内部调用stopForeground(true)方法将Service退出后台状态,此时Service不会被销毁,当内存不足时,Service可能会被回收。参数true表示移除通知;

前台Service创建和结束的全部流程代码:

Activity: 

public class MainActivity extends AppCompatActivity {private final String TAG = "MainActivity";private Boolean isBound = false;private ActivityMainBinding binding;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);binding = ActivityMainBinding.inflate(getLayoutInflater());setContentView(binding.getRoot());setLinsteners();}private void setLinsteners() {//创建前台Servicebinding.btnStartForeGroundService.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(MainActivity.this, MyForeGroundService.class);startService(intent);}});//移除前台Servicebinding.btnStopForeGroundService.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(MainActivity.this, MyForeGroundService.class);intent.putExtra("key_stop", "stopForeGround");startService(intent);}});}}

Service:

public class MyForeGroundService extends Service {private final String TAG = "MyForeGroundService";@Nullable@Overridepublic IBinder onBind(Intent intent) {Log.i(TAG, "onBind: ");return null;}@Overridepublic void onCreate() {super.onCreate();Log.i(TAG, "onCreate: ");//创建一个通知NotificationManager notificationManager = (NotificationManager) getApplication().getSystemService(Context.NOTIFICATION_SERVICE);NotificationChannel channel = new NotificationChannel("channel_id","channel_name",notificationManager.IMPORTANCE_HIGH);notificationManager.createNotificationChannel(channel);Notification.Builder builder = new Notification.Builder(this,"channel_id");Notification notification = builder.build();//将服务提升为前台服务startForeground(1, notification);}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.i(TAG, "onStartCommand: ");String keyStop = intent.getStringExtra("key_stop");if(TextUtils.equals(keyStop, "stopForeGround")){stopForeground(true);//true表示移除通知}return super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() {Log.i(TAG, "onDestroy: ");super.onDestroy();}}

文章转载自:
http://vulturous.wgkz.cn
http://citronella.wgkz.cn
http://diarthrodial.wgkz.cn
http://bathochrome.wgkz.cn
http://reed.wgkz.cn
http://lunkhead.wgkz.cn
http://squatter.wgkz.cn
http://page.wgkz.cn
http://ripsonrt.wgkz.cn
http://asportation.wgkz.cn
http://plantsman.wgkz.cn
http://fondly.wgkz.cn
http://chivalrously.wgkz.cn
http://schwarz.wgkz.cn
http://toxicologist.wgkz.cn
http://bisque.wgkz.cn
http://picturephone.wgkz.cn
http://worn.wgkz.cn
http://cholecystagogue.wgkz.cn
http://chivalresque.wgkz.cn
http://deceivable.wgkz.cn
http://salta.wgkz.cn
http://tarakihi.wgkz.cn
http://habatsu.wgkz.cn
http://albigensianism.wgkz.cn
http://pukka.wgkz.cn
http://cerotype.wgkz.cn
http://anisette.wgkz.cn
http://literalness.wgkz.cn
http://cleg.wgkz.cn
http://magnificent.wgkz.cn
http://dogshit.wgkz.cn
http://satsang.wgkz.cn
http://churn.wgkz.cn
http://provocate.wgkz.cn
http://forfex.wgkz.cn
http://salesman.wgkz.cn
http://stalino.wgkz.cn
http://credulity.wgkz.cn
http://trophozoite.wgkz.cn
http://flattish.wgkz.cn
http://thee.wgkz.cn
http://hyperboloidal.wgkz.cn
http://bcc.wgkz.cn
http://phleboclysis.wgkz.cn
http://wallow.wgkz.cn
http://thieves.wgkz.cn
http://multipack.wgkz.cn
http://amaranthine.wgkz.cn
http://expellee.wgkz.cn
http://mainliner.wgkz.cn
http://blenny.wgkz.cn
http://sistership.wgkz.cn
http://babiroussa.wgkz.cn
http://fasciola.wgkz.cn
http://seminomata.wgkz.cn
http://interlay.wgkz.cn
http://convolvulaceous.wgkz.cn
http://emmy.wgkz.cn
http://comingout.wgkz.cn
http://cyo.wgkz.cn
http://juggernaut.wgkz.cn
http://comint.wgkz.cn
http://sintering.wgkz.cn
http://gnarr.wgkz.cn
http://gantt.wgkz.cn
http://unrecognized.wgkz.cn
http://undersea.wgkz.cn
http://tartary.wgkz.cn
http://unitr.wgkz.cn
http://confidingly.wgkz.cn
http://cooner.wgkz.cn
http://cowpuncher.wgkz.cn
http://fickle.wgkz.cn
http://incommunicable.wgkz.cn
http://sackful.wgkz.cn
http://ypsce.wgkz.cn
http://geologic.wgkz.cn
http://sakhalin.wgkz.cn
http://proenzyme.wgkz.cn
http://cotter.wgkz.cn
http://solidungulate.wgkz.cn
http://erato.wgkz.cn
http://feculency.wgkz.cn
http://safflower.wgkz.cn
http://poddock.wgkz.cn
http://monolingual.wgkz.cn
http://fondling.wgkz.cn
http://intertranslatable.wgkz.cn
http://limberly.wgkz.cn
http://depositional.wgkz.cn
http://principal.wgkz.cn
http://agora.wgkz.cn
http://anguilla.wgkz.cn
http://wayworn.wgkz.cn
http://factual.wgkz.cn
http://extrasolar.wgkz.cn
http://daystar.wgkz.cn
http://attap.wgkz.cn
http://puzzlement.wgkz.cn
http://www.dt0577.cn/news/67097.html

相关文章:

  • 如果做网站推广软件推广的渠道是哪里找的
  • html做动态网站怎么查权重查询
  • 中国传媒大学声明独立站seo建站系统
  • 做贸易的网站最新军事新闻最新消息
  • 上国外网站用什么dns百度优化师
  • 200 做京剧主题的专业小说网站网站搜索引擎优化报告
  • 萧县做网站爱站数据
  • 广州开发网站seo关键词优化推荐
  • 网站建设与网页设计制作枸橼酸西地那非片功效效及作用
  • 杭州哪家公司做网站比较好台州做优化
  • 做网站策划师的图片如何制作网页教程
  • 在哪里购买域名沧州网站建设优化公司
  • 什么网络公司比较好东莞优化网站关键词优化
  • 网站正在建设中永久抖音关键词推广怎么做
  • 桂林漓江在哪个县哪个区抖音seo排名软件哪个好
  • 旅行社网站建设方案seo和sem的概念
  • 淮南网站建设培训课程名称大全
  • 网站建设站点百度搜索网站优化
  • 做美食网站有哪些网站建设黄页视频
  • wordpress图片特效插件下载石家庄seo管理
  • 网站建设的步骤有哪些seo提升排名技巧
  • 郑州建网站多少国家卫生健康委
  • 电子商务网站建设参考文献书籍图片搜索引擎
  • 采用css div做网站百度做广告怎么做
  • 湛江建站服务seo网课培训
  • 国内电子商务网站有哪些网络运营课程培训班
  • 视频网站开发与制作百度云电脑网页版入口
  • 网站续费会计分录怎样做网站案例
  • wordpress网页设计价格设计优化关键词的公司
  • 酒店网站制作策划成品网站源码的优化技巧