Android Service 启动和停止服务
2015-12-04 21:53
507 查看
activity_main.xml
定义两个Button控件,start_service和stop_service。<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:showIn="@layout/activity_main" tools:context=".MainActivity"> <Button android:id="@+id/start_service" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Start Service"/> <Button android:id="@+id/stop_service" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Stop Service"/> </LinearLayout>
MainActivity.java
同样定义两个Button控件start_service和stop_service,并设置监听器,然后用Intent对象进行通信。package jiangbin.servicetest; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends Activity implements View.OnClickListener{ private Button startService; private Button stopService; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startService = (Button) findViewById(R.id.start_service); stopService = (Button) findViewById(R.id.stop_service); startService.setOnClickListener(this); stopService.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.start_service: Intent startIntent = new Intent(this, MyService.class); startService(startIntent); break; case R.id.stop_service: Intent stopIntent = new Intent(this, MyService.class); stopService(stopIntent); default: break; } } }
MyService.java
定义Myservice类onCreate()在服务创建时候调用;
onStartCommand()在服务创建的时候调用;
onDestory()在服务销毁的时候调用。
package jiangbin.servicetest; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; public class MyService extends Service { @Override public IBinder onBind(Intent intent){ return null; } @Override public void onCreate() { super.onCreate(); Log.d("Myservice", "onCreate"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d("Myservice", "onStart"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); Log.d("Myservice", "onDestory"); } }
相关文章推荐
- Android数字游戏之数独(自动随机生成不同难度的数独)
- 从 xUtils 中发现的Android 6.0 版本更新问题
- android119 侧滑菜单
- Android开发项目(一)登录
- android的MVP模式以及接口的理解
- Android
- Android Studio 快捷键
- Android自定义组件之圆形图片
- android 布局优化(一)
- Android 图片选择器
- Android - OOM
- android 自定义按钮实现 home键 和返回键
- Android关于连接需要Portal认证的WIFI
- Android中通过浏览器打开一个网页
- Android CheckBox和RadioButton
- Android 中的 Service 全面总结
- Android笔记-APK打包流程
- Android 控件之AutoComplete(自动提示功能)
- Android打包程序
- Android LocalSocket与Socket 区别