首页 > 其他分享 >android 服务

android 服务

时间:2022-12-08 15:39:31浏览次数:32  
标签:服务 MyService Intent layout android public

 

 

1.创建服务

android 服务_android

 

android 服务_启动服务_02

Exported:是否允许除了当前程序之外的其他程序访问这个服务

Enable:是否启用这个服务

 

点击完成后自动生成

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class MyService extends Service {
public MyService() {
}

@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
}

onBind 是Service唯一的抽象方法。

创建完成后会在AndroidManifest.xml中自动进行注册

 

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

 

 

 

2.开关服务

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void startClick(View view){
Intent startIntent=new Intent(this,MyService.class);
//启动服务
startService(startIntent);
}
public void stopClick(View view){
Intent startIntent=new Intent(this,MyService.class);
//关闭服务
stopService(startIntent);
}
}

界面

android 服务_启动服务_03

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动服务"
android:onClick="startClick" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止服务"
android:onClick="stopClick" />
</LinearLayout>

 

点击启动服务按钮,在手机系统里可以发现多出一个服务名,

 

android 服务_ide_04

即使关闭程序,服务也不会停止。直到点击停止服务按钮,或者卸载了该程序。

 

3.

 

public class MyService extends Service {
public MyService() {
}

@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
//服务创建时调用
@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() {
Log.d("MyService","执行了onDestroy方法");
super.onDestroy();
}
}

 

第一次点击启动服务

android 服务_android_05

再次点击启动服务

android 服务_启动服务_06

点击停止服务

 

android 服务_ide_07

 

4.关联活动与服务

添加两个按钮

android 服务_ide_08

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="绑定服务"
android:onClick="bind" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消绑定"
android:onClick="unBind" />

 

MyService

public class MyService extends Service {

private Download dlBinder =new Download();

class Download extends Binder{

public void startDownload(){
Log.d("MyService","开始下载");
}
public int getProgress(){
Log.d("MyService","得到进度");
return 0;
}
}
@Override
public IBinder onBind(Intent intent) {
return dlBinder;

}

 

MainActivity

public class MainActivity extends AppCompatActivity {
private MyService.Download dlBinder;
//匿名类
private ServiceConnection connection =new ServiceConnection() {
//活动与服务成功绑定时调用
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//
dlBinder=(MyService.Download)service;
dlBinder.startDownload();
dlBinder.getProgress();
}
//活动与服务解除绑定时调用
@Override
public void onServiceDisconnected(ComponentName name) {
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}

public void bind(View view){
Intent bindIntent=new Intent(this,MyService.class);
//绑定服务
bindService(bindIntent,connection,BIND_AUTO_CREATE);
}

public void unBind(View view){
Intent unBindIntent=new Intent(this,MyService.class);
//解绑服务
unbindService(connection);
}
}

 点击 绑定服务

android 服务_启动服务_09

点击取消绑定

android 服务_android_10

 



标签:服务,MyService,Intent,layout,android,public
From: https://blog.51cto.com/u_13854953/5922253

相关文章

  • android build.gradle
    现在android开发SDK一般选择用最新的SDK版本,这是Google官方强烈建议的。app能运行的Android版本不是由SDK决定的,是由每一个项目的minSDK决定的。SDK都是向下兼容的。SDK......
  • android studio AndroidManifest
     一、目录结构 1.AndroidManifest.xml它是一个清单文件,提供应用的基本信息<?xmlversion="1.0"encoding="utf-8"?><!--package是android应用程序的包名,相当工程的id--......
  • android studio settings
    安装   AndroidStudio下载地址  http://www.android-studio.org/ 1、配置JDK 2、安装 AndroidStudio(带SDK)  3、配置    一、Settings (快捷键ctrl+a......
  • 5-skynet.newservice创建snlua服务
    新入门skynet系列视频b站网址https://www.bilibili.com/video/BV19d4y1678X#skynet.newservice创建snlua服务之前讲服务间请求和响应的时候,我们在main服务里启动了......
  • 服务间请求和响应
    服务间请求和响应目录服务间请求和响应snlua服务启动的基本知识发送请求收到响应这节会主要是介绍在lua服务中使用skynet.call函数snlua基本启动过程skynet.pack打包......
  • ubuntu1804搭建FTP服务器的方法
    搭建FTP服务器FTP的工作原理:FTP:FileTransferProtocol,文件传输协议。属于NAS存储的一种协议,基于CS结构。ftp采用的是双端口模式,分为命令端口和数据端口,命令端口对应命......
  • 3-logger服务
    首先我们总是要写日志的。谁还不喜欢写日志呢。我们经常这样使用skynet.error("helloworld")上面的代码就是在写日志。默认是写到stdout。当然前提是日志服务要先创......
  • nohup &退出shell终端后,服务仍然断开问题处理
    当用nohup执行任意脚本或命令,例如:nohupscp-rzczyrepos/[email protected]:/data/svnWebUI/repo/&nohup后台运行命令,关闭shell后再次进入shell发现进程在但是已经卡......
  • 使用WGCLOUD做服务指标监控可视化之监测服务器时间
    WGCLOUD监控平台有个自定义监控项模块,可以帮助我们做一些常规监控做不到的指标监测比如用来实现我们的特殊需求场景,比如监测主机的系统时间,监测某个文件的大小,定期执行计......
  • Kafka技术专题之「性能调优篇」消息队列服务端出现内存溢出OOM以及相关性能调优实战分
    内存问题本篇文章介绍Kafka处理大文件出现内存溢出java.lang.OutOfMemoryError:Directbuffermemory,主要内容包括基础应用、实用技巧、原理机制等方面,希望对大家有所帮......