0
点赞
收藏
分享

微信扫一扫

android 服务

 

 

1.创建服务

android 服务_android

 

android 服务_ide_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 服务_ide_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 服务_android_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 服务_ide_09

点击取消绑定

android 服务_android_10

 



举报

相关推荐

0 条评论