概述:
服务是需要注册的,它会在系统后台运行,即使是停止一个活动,服务仍会运行 另外,活动不宜进行耗时的操作,耗时操作应该放在一个单独的线程里。
启动服务要用到Intent。
代码:
public class TestServiceActivity extends Activity implements View.OnClickListener{
private Button mButtonStartService;
private Button mButtonStopService;
private Button mButtonStartDownload;
public static final String DOWNLOAD_SERVICE = "com.download.test";
private ProgressBar mProgressBar;
private MyReceiver mReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_service);
mButtonStartService = (Button) findViewById(R.id.button_start_service);
mButtonStopService = (Button) findViewById(R.id.button_stop_service);
mButtonStartDownload = (Button) findViewById(R.id.button_start_download);
mProgressBar = (ProgressBar) findViewById(R.id.progress_bar);
mReceiver = new MyReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(DOWNLOAD_SERVICE);
registerReceiver(mReceiver,filter);
mButtonStopService.setOnClickListener(this);
mButtonStartService.setOnClickListener(this);
mButtonStartDownload.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button_start_service:
Intent intent = new Intent(getApplicationContext(), MyReceiver.class);
startService(intent);
break;
case R.id.button_stop_service:
Intent intent1 = new Intent(getApplicationContext(), MyService.class);
stopService(intent1);
break;
case R.id.button_start_download:
Intent intent2 = new Intent(DOWNLOAD_SERVICE);
startService(intent2);
break;
default:
break;
}
}
class MyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
int count = intent.getIntExtra("count",0);
mProgressBar.setProgress(count);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mReceiver);
}
}
Service与IntentService
public class MyService extends Service {
@Override
public void onCreate() {
super.onCreate();
Log.d("service","onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("service", "onStartCommand");
new Thread(new Runnable() {
int count = 0;
@Override
public void run() {
while (count<100){
count++;
Intent intent = new Intent();
//intent.setAction(TestServiceActivity.DOWNLOAD_SERVICE);
intent.putExtra("count",count);
sendBroadcast(intent);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Log.d("service","onDestroy");
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
public class MyIntentService extends IntentService {
int count = 0;
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*
* @param name Used to name the worker thread, important only for debugging.
*/
public MyIntentService(String name) {
super(name);
}
//必须这么写,不然不能在manifests里面注册
public MyIntentService() {
super(null);
}
@Override
protected void onHandleIntent(Intent intent) {
while (count<100){
count++;
Intent intent1 = new Intent();
intent.setAction(TestServiceActivity.DOWNLOAD_SERVICE);
intent.putExtra("count",count);
sendBroadcast(intent1);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button_start_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="Start Service"/>
<Button
android:id="@+id/button_stop_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Stop Service"/>
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="match_parent"
style="?android:attr/progressBarStyleHorizontal"
android:layout_height="10dp"
android:layout_margin="10dp"/>
<Button
android:id="@+id/button_start_download"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start Download"/>
</LinearLayout>