android

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

android Service基础(启动服务与绑定服务)
Service是Android中⼀个类,它是Android 四⼤组件之⼀,使⽤Service可以在后台执⾏耗时的操作(注意需另启⼦线程),其中Service并不与⽤户产⽣UI交互。

其他的应⽤组件可以启动Service,即便⽤户切换了其他应⽤,启动的Service仍可在后台运⾏。

⼀个组件可以与Service绑定并与之交互,甚⾄是跨进程通信。

通常情况下Service可以在后台执⾏⽹络请求、播放⾳乐、执⾏⽂件读写操作或者与contentprovider交互等。

本⽂主要讲述service服务⾥的启动与绑定服务。

⾸先,XML程序如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="/apk/res/android"
xmlns:app="/apk/res-auto"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="operate"
android:text="启动服务" />
<Button
android:id="@+id/stop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="停⽌服务"
android:onClick="operate" />
<Button
android:id="@+id/bind"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="绑定服务"
android:onClick="operate" />
<Button
android:id="@+id/unbind"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="解绑服务"
android:onClick="operate"/>
</LinearLayout>
创建⼀个service类,并写创建、启动、绑定、摧毁、解绑五个⽅法
代码如下:
package com.example.service;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
private int i;
public MyService() {
}
//创建
@Override
public void onCreate() {
super.onCreate();
Log.e("TAG","服务创建了");
}
class MyBinder extends Binder {
//定义⾃⼰需要的⽅法(实现进度监控)
public int getProcess(){
return i;
}
}
//启动⽅法
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("TAG","服务启动了");
return super.onStartCommand(intent, flags, startId);
}
//解绑
@Override
public boolean onUnbind(Intent intent) {
Log.e("TAG","服务解绑了");
return super.onUnbind(intent);
}
//摧毁
@Override
public void onDestroy() {
Log.e("TAG","服务摧毁了");
super.onDestroy();
}
//绑定⽅法
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
// throw new UnsupportedOperationException("Not yet implemented");
Log.e("TAG","服务绑定了");
return new MyBinder();
}
}
然后在MainActivity⾥⾯写点击启动⽅法:
public void operate(View v) {
switch (v.getId()){
case R.id.start:
//启动服务 :创建——启动——摧毁
//如果服务已经创建了,后续重复启动,操作的都是同⼀个服务,不回在创建新的服务,除⾮先摧毁他
Intent it1=new Intent(this,MyService.class);
startService(it1);
break;
case R.id.stop:
Intent it2=new Intent(this,MyService.class);
stopService(it2);
break;
}
}
然后写绑定的⽅法:
注意:捆绑和解绑的对象应该是同⼀个对象,如果捆绑与解绑的对象不⼀样,则会报如下的错误:Service not registered: com.m1910.servicetest.MainActivity$1@41ddfcc0
本篇⽂章捆绑与解绑的对象都是conn,定义⼀个全局变量:
private ServiceConnection conn=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
};
然后写解绑与捆绑的⽅法:
case R.id.bind:
//绑定服务
Intent it3=new Intent(this,MyService.class);
bindService(it3,conn,BIND_AUTO_CREATE);
break;
case R.id.unbind:
//解绑服务
// unbindService(conn);
// if (isBound) {
// unbindService(conn);// 解绑服务
// isBound = false;
// }
unbindService(conn);//解绑服务
break;
完整的程序如下:
package com.example.service;
import androidx.appcompat.app.AppCompatActivity;
import ponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private boolean isBound = false;
private ServiceConnection conn=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(yout.activity_main);
}
public void operate(View v) {
switch (v.getId()){
case R.id.start:
//启动服务 :创建——启动——摧毁
//如果服务已经创建了,后续重复启动,操作的都是同⼀个服务,不回在创建新的服务,除⾮先摧毁他 Intent it1=new Intent(this,MyService.class);
startService(it1);
break;
case R.id.stop:
Intent it2=new Intent(this,MyService.class);
stopService(it2);
break;
case R.id.bind:
//绑定服务
Intent it3=new Intent(this,MyService.class);
bindService(it3,conn,BIND_AUTO_CREATE);
break;
case R.id.unbind:
//解绑服务
// unbindService(conn);
// if (isBound) {
// unbindService(conn);// 解绑服务
// isBound = false;
// }
unbindService(conn);//解绑服务
break;
}
}
}
到此这篇关于android Service基础(启动服务与绑定服务)的⽂章就介绍到这了,更多相关android Service内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!。

相关文档
最新文档