01_25_Service初步(一)
-
//TestActivity.java
-
package mars.service1;
-
-
import android.app.Activity;
-
import android.content.Intent;
-
import android.os.Bundle;
-
import android.view.View;
-
import android.view.View.OnClickListener;
-
import android.widget.Button;
-
-
public class TestActivity extends Activity {
-
/** Called when the activity is first created. */
-
private Button startServiceButton = null;
-
private Button stopServiceButton = null;
-
-
@Override
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.main);
-
startServiceButton = (Button) findViewById(R.id.startService);
-
startServiceButton.setOnClickListener(new StartServiceListener());
-
stopServiceButton = (Button) findViewById(R.id.stopService);
-
stopServiceButton.setOnClickListener(new StopServiceListener());
-
System.out.println("Activity onCreate");
-
}
-
-
class StartServiceListener implements OnClickListener {
-
@Override
-
public void onClick(View v) {
-
Intent intent = new Intent();
-
intent.setClass(TestActivity.this, FirstService.class);
-
startService(intent);
-
}
-
}
-
-
class StopServiceListener implements OnClickListener {
-
@Override
-
public void onClick(View v) {
-
Intent intent = new Intent();
-
intent.setClass(TestActivity.this, FirstService.class);
-
stopService(intent);
-
}
-
}
-
}
-
//FirstService.java
-
package mars.service1;
-
-
import android.app.Service;
-
import android.content.Intent;
-
import android.os.Binder;
-
import android.os.IBinder;
-
-
public class FirstService extends Service {
-
-
@Override
-
public IBinder onBind(Intent intent) {
-
System.out.println("Service onBind");
-
return null;
-
}
-
-
//当创建一个Servcie对象之后,会首先调用这个函数
-
@Override
-
public void onCreate() {
-
// TODO Auto-generated method stub
-
super.onCreate();
-
System.out.println("Service onCreate");
-
}
-
-
/*每次启动Service时都调用onStartCommand()方法*/
-
@Override
-
public int onStartCommand(Intent intent, int flags, int startId) {
-
// TODO Auto-generated method stub
-
System.out.println("flags--->" + flags);
-
System.out.println("startId--->" + startId);
-
System.out.println("Service onStartCommand");
-
return START_NOT_STICKY;
-
}
-
-
/*onDestroy()添加释放资源*/
-
@Override
-
public void onDestroy() {
-
// TODO Auto-generated method stubo
-
System.out.println("Service onDestory");
-
super.onDestroy();
-
}
-
}
点击StartService按钮->
StartService按钮->StopService->StartService按钮->StopService按钮,结果如下
04-10 00:02:34.381: I/System.out(473): Service onCreate
04-10 00:02:34.381: I/System.out(473): flags--->2
04-10 00:02:34.381: I/System.out(473): startId--->1
04-10 00:02:34.381: I/System.out(473): Service onStartCommand
04-10 00:02:40.952: I/System.out(473): flags--->2
04-10 00:02:40.952: I/System.out(473): startId--->2
04-10 00:02:40.952: I/System.out(473): Service onStartCommand
04-10 00:02:45.381: I/System.out(473): Service onDestory
04-10 00:02:46.611: I/System.out(473): Service onCreate
04-10 00:02:46.611: I/System.out(473): flags--->2
04-10 00:02:46.611: I/System.out(473): startId--->1
04-10 00:02:46.611: I/System.out(473): Service onStartCommand
04-10 00:02:48.851: I/System.out(473): Service onDestory
阅读(254) | 评论(0) | 转发(0) |