上一篇:
《Android Service(一)概述、作用、生命周期》
下一篇:《Android Service(三)》望支持加快更新!
一、startService
效果图:需要配合图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");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("Service","onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i("Service","onDestroy");
}
@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
二、bindService
效果图:需要配合图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++;
}
};
timer.schedule(timerTask,0,2000);
}
@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;
private MyConnection myConnection;
@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
|