public class ExampleService extends Service{
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
}
<service android:name=".ExampleService" android:enabled="true" android:permission="exam02.chenqian.com.servicedemo"></service>
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
// 显示启动ExampleService Intent intent = new Intent(this,ExampleService.class); // 启动ExampleService startService(intent);
@Override
public void onDestroy() {
Log.i("ServiceState","------------------>Destroy");
super.onDestroy();
}
//显示关闭Service Intent serviceIntent = new Intent(MainActivity.this,MyService.class); //关闭Service stopService(serviceIntent);
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="demo.chenqian.com.androidserverdemo.MainActivity">
<!-- 开启Service -->
<Button
android:id="@+id/btnStartService"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="@string/startService"/>
<!-- 关闭Service -->
<Button
android:id="@+id/btnStopService"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="@string/stopService"/>
<!-- 绑定Service -->
<Button
android:id="@+id/btnBindService"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="@string/bindService"/>
<!-- 解绑Service -->
<Button
android:id="@+id/btnUnbindService"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="@string/unbindService"/>
</LinearLayout>
package demo.chenqian.com.androidserverdemo;
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;
public class MyService extends Service{
/* 1、在下方我们定义了内部类MyBinder,这就是为什么我们这里现在能定义binder的原因
2、这里我们定义binder成员变量的目的是为了在下文的MainActivity中实现转型*/
private MyBinder binder = new MyBinder();
@Override
public void onCreate() {
Log.d("ServiceInfo","创建成功");
super.onCreate();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.d("ServiceInfo","绑定成功");
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("ServiceInfo","开始执行");
return super.onStartCommand(intent, flags, startId);
}
@Override
public boolean onUnbind(Intent intent) {
Log.d("ServiceInfo","解绑成功");
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
Log.d("ServiceInfo","销毁成功");
super.onDestroy();
}
public void
onServiceConnected(ComponentName
name, IBinder service),注意这里的service是IBinder类型的,我们下方获取MyService将会用到他*/
class MyBinder extends Binder{
MyService getService(){
Log.d("ServiceInfo","成功得到当前服务实例");
return MyService.this;
}
}
}
package demo.chenqian.com.androidserverdemo;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Context mContext;
private Button btnStartService;
private Button btnStopService;
private Button btnBindService;
private Button btnUnbindService;
private MyService myService;
private Intent serviceIntent;
private boolean isBond;
/*isBond该变量用来标识当前的Activity与Service是否正在绑定,因为如果不进行标识,如果Activity没有
与Service进行绑定,而执行解除绑定的操作,会照成错误或抛出异常,因为当接触绑定时Android不允许绑定为null */
/*注意,这里我们新建了一个connection并重写了相关方法,为什么我们要新建这个连接,那是因为在下方的绑定和解绑
方法即bind和unbind需要一个connection。我们复写相关方法一是为了方便观察,另一个是为了在连接成功或失去关闭
连接时,执行相关的自定义的任务或操作*/
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d("ServiceState","连接成功");
myService = ((MyService.MyBinder)service).getService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d("ServiceState","关闭连接");
//当连接指向实例为null没有指引的连接的实例时,好被虚拟机回收,降低占用的资源
myService = null;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化数据
mContext = this;
isBond = false;
//引入需要用到的组件
btnStartService = (Button) findViewById(R.id.btnStartService);
btnStopService = (Button) findViewById(R.id.btnStopService);
btnBindService = (Button) findViewById(R.id.btnBindService);
btnUnbindService = (Button) findViewById(R.id.btnUnbindService);
//为按钮添加单击事件
btnStartService.setOnClickListener(this);
btnStopService.setOnClickListener(this);
btnBindService.setOnClickListener(this);
btnUnbindService.setOnClickListener(this);
}
@Override
protected void onStart() {
serviceIntent = new Intent(this,MyService.class);
super.onStart();
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btnStartService:
//开启Service
startService(serviceIntent);
break;
case R.id.btnStopService:
//关闭Service
stopService(serviceIntent);
break;
case R.id.btnBindService:
//绑定Service
isBond = bindService(serviceIntent,connection,Context.BIND_AUTO_CREATE);
break;
case R.id.btnUnbindService:
//解绑Service,当连接为null是解绑会报错
if(isBond){
unbindService(connection);
//如果解绑成功,则修改连接标识为假
isBond = false;
}
break;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="demo.chenqian.com.androidserverdemo">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService" android:enabled="true" android:permission="demo.chenqian.com.androidserverdemo"></service>
</application>
</manifest>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有