Android——Service
xml
<?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:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.chenshuai.myapplication.ActivityService" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="测试普通服务" android:textSize="30sp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="一般启动服务" android:onClick="yibanqdonclick"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="一般停止服务" android:onClick="yibantzonclick" /> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="测试绑定服务" android:textSize="30sp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="绑定启动服务" android:onClick="bangdingqdonclick"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="绑定停止服务" android:onClick="bangdingtzonclick" /> </LinearLayout> </LinearLayout>
MyService_1.java
package com.example.chenshuai.myapplication; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Toast; public class ActivityService extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_activity_service); } //普通方式启动Service //参考Activity的启动方式 public void yibanqdonclick(View view) { //通过意图 //显示意图 Intent intent = new Intent(this,MyService_1.class); startService(intent); Toast.makeText(ActivityService.this, "启动Service", Toast.LENGTH_SHORT).show(); } public void yibantzonclick(View view) { //通过意图 //显示意图 Intent intent = new Intent(this,MyService_1.class); stopService(intent); Toast.makeText(ActivityService.this, "启动Service", Toast.LENGTH_SHORT).show(); } ServiceConnection serviceConnection; public void bangdingqdonclick(View view) { Intent intent = new Intent(this,MyService_1.class); //实现ServiceConnection接口 if (serviceConnection == null) { serviceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.e("TAG", "连接服务"); } @Override public void onServiceDisconnected(ComponentName name) { Log.e("TAG", "断开连接"); } }; } //参数:意图,连接,连接方式 BIND_AUTO_CREATE自动创建 bindService(intent,serviceConnection, Context.BIND_AUTO_CREATE); Toast.makeText(ActivityService.this, "连接服务成功", Toast.LENGTH_SHORT).show(); } public void bangdingtzonclick(View view) { //判断是否已经绑定,连接是否为空 if (serviceConnection != null) { //解除绑定 unbindService(serviceConnection); serviceConnection = null; Toast.makeText(ActivityService.this, "解除绑定", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(ActivityService.this, "尚未绑定", Toast.LENGTH_SHORT).show(); } } //重写回调方法,防止强制退出时,绑定服务还在运行 @Override protected void onDestroy() { if (serviceConnection != null) { //解除绑定 unbindService(serviceConnection); serviceConnection = null; } super.onDestroy(); } }
ActivityService.java
package com.example.chenshuai.myapplication; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Toast; public class ActivityService extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_activity_service); } //普通方式启动Service //参考Activity的启动方式 public void yibanqdonclick(View view) { //通过意图 //显示意图 Intent intent = new Intent(this,MyService_1.class); startService(intent); Toast.makeText(ActivityService.this, "启动Service", Toast.LENGTH_SHORT).show(); } public void yibantzonclick(View view) { //通过意图 //显示意图 Intent intent = new Intent(this,MyService_1.class); stopService(intent); Toast.makeText(ActivityService.this, "启动Service", Toast.LENGTH_SHORT).show(); } ServiceConnection serviceConnection; public void bangdingqdonclick(View view) { Intent intent = new Intent(this,MyService_1.class); //实现ServiceConnection接口 if (serviceConnection == null) { serviceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.e("TAG", "连接服务"); } @Override public void onServiceDisconnected(ComponentName name) { Log.e("TAG", "断开连接"); } }; } //参数:意图,连接,连接方式 BIND_AUTO_CREATE自动创建 bindService(intent,serviceConnection, Context.BIND_AUTO_CREATE); Toast.makeText(ActivityService.this, "连接服务成功", Toast.LENGTH_SHORT).show(); } public void bangdingtzonclick(View view) { //判断是否已经绑定,连接是否为空 if (serviceConnection != null) { //解除绑定 unbindService(serviceConnection); serviceConnection = null; Toast.makeText(ActivityService.this, "解除绑定", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(ActivityService.this, "尚未绑定", Toast.LENGTH_SHORT).show(); } } //重写回调方法,防止强制退出时,绑定服务还在运行 @Override protected void onDestroy() { if (serviceConnection != null) { //解除绑定 unbindService(serviceConnection); serviceConnection = null; } super.onDestroy(); } }
minifest.xml
<service android:name=".MyService_1" android:enabled="true" android:exported="true" />