赞
踩
效果图:需要配合图2查看效果,往下翻
图1
图2
图3
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button btnStart,btnStop; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { btnStart = findViewById(R.id.btn_start); btnStop = findViewById(R.id.btn_stop); btnStart.setOnClickListener(this); btnStop.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn_start: Intent intent1 = new Intent(MainActivity.this,MyService.class); startService(intent1); break; case R.id.btn_stop: Intent intent2 = new Intent(MainActivity.this,MyService.class); stopService(intent2); break; } } }
MyService.java
public class MyService extends Service { public MyService() { } //创建服务时,系统会调用此方法。如果服务已经运行,则不会调用此方法,所以该方法只调用一次 @Override public void onCreate() { super.onCreate(); Log.i("Service","onCreate"); } //当组件调用 startService()请求启动服务时,系统将调用此方法 @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i("Service","onStartCommand"); return super.onStartCommand(intent, flags, startId); } //当服务不再使用且将被销毁时,比如:组件调用 stopService(),系统将会调用此方法 @Override public void onDestroy() { super.onDestroy(); Log.i("Service","onDestroy"); } //源码:将通信通道返回给服务 //将客户端和服务端进行绑定,若不能绑定,则返回null @Override public IBinder onBind(Intent intent) { return null; } }
AndroidManifest.xml
<service
android:name=".MyService"
android:enabled="true"
android:exported="true"></service>
adtivity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <Button android:id="@+id/btn_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:layout_marginTop="80dp" android:layout_gravity="center" android:text="开始"/> <Button android:id="@+id/btn_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="80dp" android:textSize="20dp" android:text="结束"/> </LinearLayout>
源码:
链接:https://pan.baidu.com/s/112ub3K6HxtJ7YWjTveaV_A
提取码:dljv
效果图:需要配合图2查看效果,往下翻
图1
图2
图3
MyService.java
public class MyService extends Service { private MyBinder myBinder; private int num = 0; //数字 private Timer timer; private TimerTask timerTask; public MyService() { } //创建服务时,系统会调用此方法。如果服务已经运行,则不会调用此方法,所以该方法只调用一次 @Override public void onCreate() { super.onCreate(); Log.i("MyBindService","onCreate"); myBinder = new MyBinder(); //定时器 timer = new Timer(); //定时器任务 timerTask = new TimerTask() { @Override public void run() { num++; } }; //先立即执行 timerTask里的任务,然后每个两秒再继续去执行 timerTask里的任务。 timer.schedule(timerTask,0,2000); } //将客户端和服务端进行通信绑定 //return IBinder对象,这个对象所指的就是客户端和服务端的通信通道 @Override public IBinder onBind(Intent intent) { Toast.makeText(this,"绑定",Toast.LENGTH_SHORT).show(); Log.i("MyBindService","onBind"); return myBinder; } //解除绑定 @Override public boolean onUnbind(Intent intent) { Log.i("MyBindService","onUnbind"); return super.onUnbind(intent); } //销毁 @Override public void onDestroy() { super.onDestroy(); timer.cancel(); timerTask.cancel(); Log.i("MyBindService","onDestroy"); } public class MyBinder extends Binder{ public int getNum(){ return num; } } }
AndroidManifest.xml
<service
android:name=".MyService"
android:enabled="true"
android:exported="true"></service>
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button btnStart,btnGetValue,btnStop; private MyService.MyBinder myBinder; //创建 Binder 对象 ->通信管道(通信媒介) private MyConnection myConnection; //创建 ServiceConnection 对象 ->连接服务 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); myConnection = new MyConnection(); } private void init() { btnStart = findViewById(R.id.btn_start); btnGetValue = findViewById(R.id.btn_get_value); btnStop = findViewById(R.id.btn_stop); btnStart.setOnClickListener(this); btnGetValue.setOnClickListener(this); btnStop.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn_start: Intent intent = new Intent(MainActivity.this,MyService.class); //意图,服务连接,绑定方式 bindService(intent,myConnection, Service.BIND_AUTO_CREATE); break; case R.id.btn_get_value: android.util.Log.i("MyBindService","MyBinder 还存在吗? = " + myBinder); if (myBinder!=null) Toast.makeText(MainActivity.this,String.valueOf(myBinder.getNum()),Toast.LENGTH_SHORT).show(); else Toast.makeText(MainActivity.this,"不好意思,服务已断开,请重新连接",Toast.LENGTH_SHORT).show(); break; case R.id.btn_stop: //解除绑定 unbindService(myConnection); myBinder = null; break; } } public class MyConnection implements ServiceConnection { //服务连接 @Override public void onServiceConnected(ComponentName name, IBinder service) { myBinder = (MyService.MyBinder) service; Log.i("MyBindService","onServiceConnected"); } //服务断开连接 //只会在出现异常时才会被调用 @Override public void onServiceDisconnected(ComponentName name) { Log.i("MyBindService","onServiceDisconnected"); } } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <Button android:id="@+id/btn_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:layout_marginTop="80dp" android:layout_gravity="center" android:text="开始"/> <Button android:id="@+id/btn_get_value" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="80dp" android:textSize="20dp" android:text="查看结果"/> <Button android:id="@+id/btn_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="80dp" android:textSize="20dp" android:text="结束"/> </LinearLayout>
源码:
链接:https://pan.baidu.com/s/1cid7MKWdgeIXe2Hz7Pef7w
提取码:rjqw
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。