Android Studio 四大组件之 Service的生命周期上
来源:互联网 发布:俄罗斯方块c语言程序 编辑:程序博客网 时间:2024/06/06 08:43
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 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时的生命周期。
0 0
- Android Studio 四大组件之 Service的生命周期上
- Android 四大组件之Service 的生命周期与启动Service
- Android 四大组件之Service 生命周期
- Android 四大组件之Service 的生命周期和使用
- Android 四大组件之Service 的生命周期和使用
- Android 四大组件生命周期:Service
- Android 四大组件之 Service (上)
- Android四大组件之Service第一章:service及其生命周期
- Android四大组件之Actiivty的生命周期
- Android四大组件之Activity的生命周期
- Android四大组件之Service的使用
- Android四大组件之Service的介绍
- android的四大组件之Service
- 四大组件之Service 生命周期(3)
- Android四大组件之 Service
- Android四大组件之 Service
- Android四大组件之 Service
- android四大组件之Service
- java学习日记【9-27复习】
- js中div循环转动的动画代码
- java性能优化笔记(一)概述
- css深入理解之folat浮动1-1
- 单片机(二)__seg数码管
- Android Studio 四大组件之 Service的生命周期上
- 【OVS2.5.0源码分析】sFlow实现分析(1)
- MySQL 授权远程连接
- css深入理解之folat浮动 2-1包裹与破坏
- java实现阿拉伯数字转人民币大写(精确到分)
- Genymotion模拟器如何访问电脑本地的服务器
- Dom事件处理程序
- 设置phpMyAdmin可管理多个数据库服务器、自动登录等操作
- 根据起始IP和结束IP输出指定范围的IP