第一行代码 第10章 服务 -- 基本用法
2017-07-05 23:45
323 查看
服务的基本用法
AndroidManifest .xml
MyService.java
activity_main.xml
MainActivity.java
1、MyService中重写了onCreate()、onStartCommand()和onDestroy()
其中onCreate()方法是在服务创建的时候调用;
onStartCommand()方法是在每次启动服务的时候调用;
onDestroy()方法是在服务销毁的时候调用。
2、需要在AndroidManifest.xml中注册。
3、通过构建Intent对象,并调用startService()方法来启动活动。
4、通过调用stopSelf()方法来实现自动停止服务
AndroidManifest .xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.sky.servicedemo"> <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>
MyService.java
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.d("MyService", "onCreate executed"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d("MyService", "onStartCommand executed"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); Log.d("MyService", "onDestroy executed"); } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.sky.servicedemo.MainActivity"> <Button android:id="@+id/btnStartService" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="start service" /> <Button android:id="@+id/btnStopService" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/btnStartService" android:text="stop service" /> </RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button btnStartService; private Button btnStopService; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnStartService = (Button)findViewById(R.id.btnStartService); btnStopService = (Button)findViewById(R.id.btnStopService); btnStartService.setOnClickListener(this); btnStopService.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.btnStartService: Intent startServiceIntent = new Intent(this, MyService.class); startService(startServiceIntent); break; case R.id.btnStopService: Intent stopServiceIntent = new Intent(this, MyService.class); stopService(stopServiceIntent); break; default: break; } } }
1、MyService中重写了onCreate()、onStartCommand()和onDestroy()
其中onCreate()方法是在服务创建的时候调用;
onStartCommand()方法是在每次启动服务的时候调用;
onDestroy()方法是在服务销毁的时候调用。
2、需要在AndroidManifest.xml中注册。
3、通过构建Intent对象,并调用startService()方法来启动活动。
4、通过调用stopSelf()方法来实现自动停止服务
相关文章推荐
- 第一行代码总结:9.3服务的基本用法——启动和停止服务
- 第一行代码-9.3 服务的基本用法
- 第一行代码总结:9.3服务的基本用法——活动和服务进行通信
- 第一行代码总结:9.3服务的基本用法——定义一个服务
- 第一行代码总结:9.3服务的基本用法——服务的更多技巧(使用前台服务)
- 第一行代码总结:9.3服务的基本用法——使用IntentService
- 第一行代码阅读笔记----显示隐式Intent的基本用法
- 第一行代码 第10章 服务 -- 生命周期
- 第一行代码:线程的基本用法总结
- 第一行代码阅读笔记----显示隐式Intent的基本用法
- 第一行代码笔记9(通知的基本用法)
- 第一行代码学习笔记-第二章 探究活动-1.活动的基本用法
- 第一行代码笔记 service基本用法
- android第一行代码-2.activity基本用法
- 第一行代码 第10章 服务 -- 活动与服务进行通信
- 第一行代码 第10章 服务 -- 前台服务与IntentService
- 第一行代码阅读笔记----显示隐式Intent的基本用法
- 第一行代码--android学习笔记1-----活动的基本用法
- jquery validate.js表单验证的基本用法入门--不用写繁琐的验证代码了...
- Intent的用法——第一行代码阅读笔记