MainActivity:
package com.itheima.testservice;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view){
Intent intent = new Intent(this,MyService.class);
startService(intent);
}
}
----------------------------------
MyService:
package com.itheima.testservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
//oncreate ondestory onstart onstop onresume onpause
//服务没有界面
@Override
public void onCreate() {
System.out.println("服务创建了");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("服务接收到了开启指令");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
System.out.println("服务销毁了");
super.onDestroy();
}
}