1. 定义Service类
@Override
public IBinder onBind(Intent intent)
通过Override三个函数来学期Serivce的启动
public void onCreate()
public int onStartCommand(Intent intent, int flags, int startId)
public void onDestroy()
public void onCreate()
public int onStartCommand(Intent intent, int flags, int startId)
类定义
public class MyService extends Service {
public static final String TAG = "MyService";
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate() executed");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand() executed");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy() executed");
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
在
Intent intent = new Intent(this, ClassName.class);
startService(intent);
[ 停止Service ]
在Activity中停止Service
Intent intent = new Intent(this, ClassName.class);
stopService(intent);