赵雅智:service_startService生命周期
2014-06-20 14:59
246 查看
案例演示
布局文件
<RelativeLayout 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" tools:context="${packageName}.${activityClass}" > <Button android:id="@+id/btn_start" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="click" android:text="context.startService" /> <Button android:id="@+id/btn_stop" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/btn_start" android:onClick="click" android:text="context.strpService" /> </RelativeLayout>
注册service
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android_servicedemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.android_servicedemo.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name="ServiceTese" android:permission="com.example.permission.servers" > <intent-filter> <action android:name="com.example.serversdemo.action" /> </intent-filter> </service> </application> </manifest>
主activity
package com.example.android_servicedemo; import android.app.Activity; import android.app.Service; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.util.Log; import android.view.View; public class MainActivity extends Activity { private Intent service; MyConn myConn; public static final String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 隐士意图的启动 service = new Intent(); service.setAction("com.example.serversdemo.action"); } public void click(View v) { int id = v.getId(); switch (id) { /* * 启动服务 */ case R.id.btn_start: this.startService(service); break; /* * 停止服务 */ case R.id.btn_stop: this.stopService(service); break; default: break; } } }
运行结果
点击context.startService--->点击context.stopService。生命周期为点击context.startService--->home键--->点击context.startService--->点击context.startService...--->点击context.stopService
onbindService见/article/1334019.html
相关文章推荐
- 赵雅智:service_startService生命周期
- Serivce生命周期-先startService,然后bindService
- 赵雅智_service生命周期
- Android中startService的使用及Service生命周期
- Android 面试题笔记-------bindservice与startservice的生命周期?
- Android Service生命周期 Service里面的onStartCommand()方法详解
- Android Service生命周期 Service里面的onStartCommand()方法详解
- Android Service生命周期 Service里面的onStartCommand()方法详解
- 关于服务Service使用startService()函数启动时的生命周期
- Startservice的生命周期
- startService与bindService混合使用对Service生命周期的影响
- 有关Service的startService与bindService在各种情形下生命周期的变化
- Android-Service (基本知识,生命周期,实例-startService 启动的服务音乐播放器后台服务播放)
- Service之startService,bindService交叉使用时的生命周期
- startService与bindService混合使用对Service生命周期的影响
- Android中startService的使用及Service生命周期
- Android中startService的使用及Service生命周期
- Android Service生命周期 Service里面的onStartCommand()方法详解
- Android中startService的使用及Service生命周期
- Android Service生命周期 Service里面的onStartCommand()方法详解