您的位置:首页 > 移动开发 > Android开发

Android Studio 四大组件之 Service的生命周期上

2016-09-09 21:48 281 查看
1.Service创建方式 ,new - Service - Service,如下图:


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



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

代码如下:

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

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.my.service">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

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

</manifest>

自己注册的话只需要写上
<service android:name=".MyService"></service>

这句话就可以了。

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

代码如下:

activity_main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">
<Button
android:id="@+id/start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="startService"
android:text="start Service" />
<Button
android:id="@+id/stop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="stopService"
android:text="stop Service" />
</LinearLayout>


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 M
a3db
ainActivity 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时的生命周期。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android studio Serivce