美文网首页
Android Service的基本用法

Android Service的基本用法

作者: 好学人 | 来源:发表于2019-09-25 22:46 被阅读0次

1. 创建一个Service的子类

public class MyService extends Service {

    @Override
    public void onCreate() {
        super.onCreate();
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }


    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}

2. 在manifest中配置Service

<service
    android:name=".MyService"
    android:enabled="true"
    android:exported="false"/>

3. 启动与关闭Service

// 启动Service
Intent intent = new Intent(context, MyService.class);
context.startService(intent);
// 关闭Service
Intent intent = new Intent(context, MyService.class);
context.stopService(intent);

相关文章

网友评论

      本文标题:Android Service的基本用法

      本文链接:https://www.haomeiwen.com/subject/nsawuctx.html