Android Studio 四大组件之 Service的生命周期上
2016-09-09 21:48
281 查看
1.Service创建方式 ,new - Service - Service,如下图:
![](https://img-blog.csdn.net/20160909215202423?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
然后直接点击finish就可以了。
![](https://img-blog.csdn.net/20160909215611366?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
2.通过startService启动方式,系统自动生成了一个构造方法,和一个onBind()方法。要复写onCreate(),onStartCommand(),onDestory().这是Service的生命周期。主要耗时的操作都在onStartCommand()中执行。
代码如下:
①.首先要在清单文件(AndroidManifest.xml)中进行注册(按照我的方法,可以在清单文件中自动注册),以下是系统自动生成的。如果没有需要自己去注册。
自己注册的话只需要写上
这句话就可以了。
②.如果组件通过调用startService()启动服务(会调用onStartCommand()),则服务将一直运行,直到服务使用stopSelf(),或由其他组件通过调用stopService() 停止它为止。
代码如下:
activity_main.xml文件
当点击startService按钮时,Log打印下面的语句
![](https://img-blog.csdn.net/20160910094154956?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
当点击stopService按钮时,Log打印下面的语句
![](https://img-blog.csdn.net/20160910094252879?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
以上就是用startService()方法开启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四大组件——Service 生命周期详解
- Android 四大组件之Service 的生命周期和使用
- Android 四大组件之Service 的生命周期与启动Service
- 四大组件之Service(二)-Service在各种情况下的生命周期
- 四大组件----Service的生命周期
- 安卓四大组件之二#1-Service的创建,生命周期以及动态调用Service里的方法 推荐
- android四大基础组件--Service生命周期详解
- 四大组件---service生命周期事例详解(二)
- Android 四大组件之Service 生命周期
- Android 四大组件之Service 的生命周期和使用
- 四大组件---Service的生命周期(一)
- Android 四大组件生命周期:Service
- Android四大组件之Service精通
- Android四大基本组件介绍与生命周期
- Android四大基本组件介绍与生命周期
- Android四大基本组件介绍与生命周期
- android四大组件之Service
- Android四大组件:Activity、Service、Broadcast Receiver、Content Provider
- 安卓四大组件和生命周期
- Android四大组件之Service(服务)(二)