Android开发学习之路--Service之初体验
android最后一个组件便是service了,终于学习到最后一个组件了,从年前的开发环境的搭建,到现在学到最后一个组件花了三周的时间,期间记录的点点滴滴,照着书本学习编写的代码都受益匪浅,这里要感谢第一行代码这本书。三个星期除了三十和初一没有学习,其余时间坚持每天学习一个知识点,总算慢慢地学习了个大概,接下去继续学习其他的东西。说了这么多还是开始学习Service组件吧。
Service从字面上理解就是服务的意思,也就是为应用程序提供服务,比如我们在播放一首歌的时候,我又想把它下载下来,那么我们就可以把他交给Service去处理。播放歌曲是在后台运行的,那么它也是个服务,还是来个例子吧。先新建工程ServiceTest。编写布局如下:
<!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%3F%2D%2D%3E-->
<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:orientation="vertical" android:layout_margin="10dp" android:padding="10dp" tools:context="com.example.jared.servicetest.MainActivity"><button android:id="@+id/startService" android:text="启动Service" android:layout_width="match_parent" android:layout_height="wrap_content" android:textallcaps="false"></button><button android:id="@+id/stopService" android:text="停止Service" android:layout_width="match_parent" android:layout_height="wrap_content" android:textallcaps="false"></button></linearlayout>
这里实现了两个按钮启动和停止Service。接着新建一个MyService类,继承Service,代码如下:
package com.example.jared.servicetest;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;
/**
* Created by jared on 16/2/18.
*/
public class MyService extends Service{
private static final String TAG = "MyService";
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate is called");
Toast.makeText(MyService.this, "onCreate is called",Toast.LENGTH_SHORT).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand is called");
Toast.makeText(MyService.this, "onStartCommand is called",Toast.LENGTH_SHORT).show();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy is called");
Toast.makeText(MyService.this, "onDestroy is called",Toast.LENGTH_SHORT).show();
}
}
这里重写了onCreate方法,onStartCommand方法和onDestroy方法,这里在各个方法里加了些调试信息,接着我们再实现MainActivity的代码:
package com.example.jared.servicetest;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button mStartServiceBtn;
private Button mStopServiceBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mStartServiceBtn = (Button)findViewById(R.id.startService);
mStopServiceBtn = (Button)findViewById(R.id.stopService);
mStartServiceBtn.setOnClickListener(new myOnClickListener());
mStopServiceBtn.setOnClickListener(new myOnClickListener());
}
private class myOnClickListener implements View.OnClickListener {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.startService:
Intent mStartIntent = new Intent(getApplicationContext(), MyService.class);
startService(mStartIntent);
break;
case R.id.stopService:
Intent mSopIntent = new Intent(getApplicationContext(), MyService.class);
stopService(mSopIntent);
break;
default:
break;
}
}
}
}
这里当按start的时候启动service,按stop的按钮的时候停止服务。先来运行下看下效果,先启动Service吧,效果如下:

从上可知先调用了onCreate方法,然后调用onStartCommand方法。然后我们看下在运行着的服务,打开手机的setting下的apps里的running:

从图可知ServiceTest已经在了,然后这个时候我们再点击启动Service的话,是不会再调用onCreate方法的,只会调用onStartCommand方法,只有stop了Service才可以,那我们先stop掉Service,效果如下:

如图调用了onDestroy方法。接着再启动Service的话,就和上述一样了。
上面代码中还有个方法那就是onBind方法了,从字面可以理解到这个方法是把Activity和Service绑定了,那样就可以通过Activity控制Service了。下面实现下代码,修改layout如下:
<!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%3F%2D%2D%3E-->
<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:orientation="vertical" android:layout_margin="10dp" android:padding="10dp" tools:context="com.example.jared.servicetest.MainActivity"><button android:id="@+id/startService" android:text="启动Service" android:layout_width="match_parent" android:layout_height="wrap_content" android:textallcaps="false"></button><button android:id="@+id/stopService" android:text="停止Service" android:layout_width="match_parent" android:layout_height="wrap_content" android:textallcaps="false"></button><button android:id="@+id/startDownloadService" android:text="绑定Service Download" android:layout_width="match_parent" android:layout_height="wrap_content" android:textallcaps="false"></button><button android:id="@+id/stopDownloadService" android:text="解绑Service Download" android:layout_width="match_parent" android:layout_height="wrap_content" android:textallcaps="false"></button><button android:id="@+id/startGetProgressService" android:text="绑定Service GetProgress" android:layout_width="match_parent" android:layout_height="wrap_content" android:textallcaps="false"></button><button android:id="@+id/stopGetProgressService" android:text="解绑Service GetProgress" android:layout_width="match_parent" android:layout_height="wrap_content" android:textallcaps="false"></button></linearlayout>
这里添加了启动下载的服务和获取当前下载状态的服务。Service中添加Binder,代码如下:
package com.example.jared.servicetest; import android.app.Service; import android.content.Intent; import

