1.Service创建方式 ,new - Service - Service,如下图:

然后直接点击finish就可以了。

2.通过startService启动方式,系统自动生成了一个构造方法,和一个onBind()方法。要复写onCreate(),onStartCommand(),onDestory().这是Service的生命周期。主要耗时的操作都在onStartCommand()中执行。

代码如下:

①.首先要在清单文件(AndroidManifest.xml)中进行注册(按照我的方法,可以在清单文件中自动注册),以下是系统自动生成的。如果没有需要自己去注册。

<?xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.my.service">            android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@style/AppTheme">        android:name=".MainActivity">                            android:name="android.intent.action.MAIN" />                android:name="android.intent.category.LAUNCHER" />                                        android:name=".MyService"            android:enabled="true"            android:exported="true">            
自己注册的话只需要写上
 android:name=".MyService">
这句话就可以了。

②.如果组件通过调用startService()启动服务(会调用onStartCommand()),则服务将一直运行,直到服务使用stopSelf(),或由其他组件通过调用stopService() 停止它为止。

代码如下:

activity_main.xml文件

<?xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.example.my.service.MainActivity">            android:id="@+id/start"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:onClick="startService"        android:text="start Service" />            android:id="@+id/stop"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:onClick="stopService"        android:text="stop Service" />

package com.example.my.service;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    public void startService(View view) {    startService(new Intent(MainActivity.this,MyService.class));    }    public void stopService(View view) {    stopService(new Intent(MainActivity.this,MyService.class));    }}
package com.example.my.service;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.util.Log;public class MyService extends Service {    public MyService() {    }    @Override    public IBinder onBind(Intent intent) {        // TODO: Return the communication channel to the service.        throw new UnsupportedOperationException("Not yet implemented");    }    @Override    public void onCreate() {        super.onCreate();        Log.e("TAG","onCreate方法执行了");    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        Log.e("TAG","onStartCommand方法执行了");        return super.onStartCommand(intent, flags, startId);    }    @Override    public void onDestroy() {        super.onDestroy();        Log.e("TAG","onDestroy方法执行了");    }}
当点击startService按钮时,Log打印下面的语句


当点击stopService按钮时,Log打印下面的语句


以上就是用startService()方法开启Service时的生命周期。


更多相关文章

  1. Android在onCreate()方法中动态获取TextView控件的高度
  2. Android两种不同的方法去实现图像的放大与缩小
  3. 【Android】布局
  4. Android(安卓)setContentView方法解析(二)
  5. camera的startpreview流程
  6. 简述Android(安卓)Android(安卓)studio快捷键
  7. 【Android】理解Service和BroadcastReceiver
  8. Android常用代码之APK root权限静默安装
  9. Android(安卓)TextView 添加下划线的简单方法

随机推荐

  1. 用 S5PV210 学习 Linux (二) 刷机(二)
  2. linux挂载分区后重启失败
  3. 视频去广告及商用软件Vip功能破解方法
  4. 确定shell程序中的相对路径或绝对路径
  5. 20169210《Linux内核原理与分析》第七周
  6. Bash脚本删除目录中多个文件名末尾的'x'
  7. 自己实现的C++智能指针的功能代码和测试
  8. Linux 内存寻址之分段机制
  9. 我如何理解这个编译器错误:“...的多重定
  10. [日常] Linux使用diff来比较目录