启动形式Service
定义一个类HelloIntentService:
package com.luohaibo.testdemo11; import android.app.IntentService; import android.content.Intent; public class HelloIntentService extends IntentService { public HelloIntentService() { // TODO Auto-generated constructor stub super("HelloIntentService"); } @Override protected void onHandleIntent(Intent intent) { // TODO Auto-generated method stub System.out.println("休息5秒!"); try { Thread.sleep(8000); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } @Override public void onDestroy() { // TODO Auto-generated method stub System.out.println("执行完onHandleIntent之后自动调用"); super.onDestroy(); } }
定义一个类HelloService:
package com.luohaibo.testdemo11; import android.app.Service; import android.content.Intent; import android.os.IBinder; public class HelloService extends Service { public HelloService() { // TODO Auto-generated constructor stub } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); } @Override public void onStart(Intent intent, int startId) { // TODO Auto-generated method stub super.onStart(intent, startId); System.out.println("启动Service,休眠10秒"); try { Thread.sleep(10000); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } }
main代码如下:
package com.luohaibo.testdemo11; import com.luohaibo.testdemo11.R.id; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.Button; import android.view.View.OnClickListener; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button =(Button)findViewById(R.id.button1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(MainActivity.this,HelloIntentService.class); startService(intent); } }); Button button2 = (Button)findViewById(R.id.button2); button2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(MainActivity.this,HelloIntentService.class); stopService(intent); } }); Button button3 = (Button)findViewById(R.id.button3); button3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(MainActivity.this, HelloService.class); startService(intent); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="启动IntentService"/> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="停止IntentService"/> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="启动Service"/> </LinearLayout>
需要在AndroidManifest.xml中声明Service
<service android:name="com.luohaibo.testdemo11.HelloService"></service> <service android:name="com.luohaibo.testdemo11.HelloIntentService"></service>