您的位置:首页 > 其它

Service的开启和停止以及生命周期

2016-03-24 18:05 316 查看
1、清单文件

<service android:name=".TestService"></service>


2、开启Service

Intent intent = new Intent(this, TestService.class);
startService(intent);


3、停止服务

Intent intent = new Intent(this, TestService.class);
stopService(intent);


4、Service的开启和停止所显示的生命周期

package com.example.testservice;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class TestService extends Service {

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}

@Override
public void onCreate() {
// TODO Auto-generated method stub--1
System.out.println("onCreate");
super.onCreate();

}

@Override
@Deprecated
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub--3
System.out.println("onStart");
super.onStart(intent, startId);

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub--2
System.out.println("onStartCommand");
return super.onStartCommand(intent, flags, startId);

}

@Override
public void onDestroy() {
// TODO Auto-generated method stub--4
System.out.println("onDestroy");
super.onDestroy();
}

}


注意:--n显示是它们的执行顺序
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: