Service的肤浅了解
来源:互联网 发布:c语言宿舍管理系统 编辑:程序博客网 时间:2024/05/21 12:50
Service就我自己理解就是当你退出程序之后,程序还可以在后台运行的控件
最简单的实现Service方法 就是创建一个类继承Service
发现它跟Activity有点类似 因为Service也有oncreate
onstart ondestory的方法(其他的就没有了)
打开源代码 Service竟然有这么多方法
额 但是现在用一些最常用的方法 onBind onUnbind
onStartCommand 这些常用的方法
了解一个控件就需要了解这些方法在什么时候执行(我指的是我自己常用的那些)
就我现在所知启动Service有两种方法bindService和StartService
于是就设置4个按钮
<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=".MainActivity"> <TextView android:id="@+id/textshow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="show me "/> <Button android:id="@+id/startservice" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="startService" /> <Button android:id="@+id/stopservice" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="stopService" /> <Button android:id="@+id/bindservice" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="bindService" /> <Button android:id="@+id/unbindservice" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="unbindService" /></LinearLayout>
MainActivity
package com.example.administrator.calculator;import android.app.Activity;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.TextView;public class MainActivity extends Activity implements View.OnClickListener{// 当我们想获取启动的Service实例时,我们可以用到bindService和onBindService方法,它们分别执行了Service中IBinder()和onUnbind()方法。 private Button startService,stopService,bindService,unbindService; private TestService testService; private TextView tv; private Context mcontext; //这里需要用到ServiceConnection在Context.bindService和context.unBindService()里用到 private ServiceConnection mServiceConnection = new ServiceConnection() { //当我bindService时,让TextView显示MyService里getSystemTime()方法的返回值 @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.e("MYSERVICEmAIN","onServiceConnected"); testService = ((TestService.MyBinder)service).getService(); tv.setText("I am from Service :" + testService.getString()); } @Override public void onServiceDisconnected(ComponentName name) { Log.e("MYSERVICEmAIN","onServiceDisconnected"); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setupView(); } private void setupView() { mcontext = MainActivity.this; tv = (TextView) findViewById(R.id.textshow); startService = (Button) findViewById(R.id.startservice); stopService = (Button) findViewById(R.id.stopservice); bindService = (Button) findViewById(R.id.bindservice); unbindService = (Button) findViewById(R.id.unbindservice); startService.setOnClickListener(this); stopService.setOnClickListener(this); bindService.setOnClickListener(this); unbindService.setOnClickListener(this);; } @Override public void onClick(View v) { Intent i; if(v==startService){ i = new Intent(MainActivity.this,TestService.class); mcontext.startService(i); }else if(v==stopService){ i = new Intent(MainActivity.this,TestService.class); mcontext.stopService(i); }else if(v==bindService){ i = new Intent(MainActivity.this,TestService.class); mcontext.startService(i); mcontext.bindService(i,mServiceConnection,BIND_AUTO_CREATE); }else{ mcontext.unbindService(mServiceConnection); } }}
TestService.java
package com.example.administrator.calculator;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.support.annotation.Nullable;import android.text.format.Time;import android.util.Log;/** * Created by Administrator on 2015/11/21. */public class TestService extends Service{// 定义一个Tag标签 private static final String TAG = "MYSERVICE";// 这里定义一个Binder 用在onBind()方法,这样Activity那边好、可以获取到 private MyBinder myBinder = new MyBinder(); @Nullable @Override public IBinder onBind(Intent intent) { Log.e(TAG,"onBind"); return myBinder; } @Override public void onCreate() { Log.e(TAG,"onCreate"); super.onCreate(); } @Override public void onStart(Intent intent, int startId) { Log.e(TAG,"onStart"); super.onStart(intent, startId); } @Override public void onDestroy() { Log.e(TAG,"onDestroy"); super.onDestroy(); } @Override public boolean onUnbind(Intent intent) { Log.e(TAG,"onUnbind"); return super.onUnbind(intent); } public String getString(){ return "获取到Service实例"; } public class MyBinder extends Binder{ TestService getService(){ return TestService.this; } } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.e(TAG,"onStartCommand"); return super.onStartCommand(intent, flags, startId); }}
按顺序按按钮
结果
今天就到此为止
0 0
- Service的肤浅了解
- 一些肤浅的东西
- NHibernate的肤浅认识
- c++重写的肤浅理解
- 厌恶肤浅喋喋不休的人
- RSA加密算法的肤浅理解
- 肤浅的模仿是虚假
- android 的service一点了解
- 简单了解Android的Service
- Service需要了解的理论知识
- 对C++的一点肤浅的认知
- socket编程的一个肤浅的认识
- 关于ElasticSearch的一些肤浅的思考
- cocos2dx的Render Command的肤浅见解
- 第九篇--Linkify的肤浅研究
- 对象的肤浅认识和工厂模式
- 了解glibc的Name Service Switch
- 肤浅的语言只会制造一个粗略的程序员
- 多媒体封装格式学习:H264封装成FLV(二)
- js数组存储对象及对象的获取
- 获取字符串中字母出现的个数
- HDU 2070 叠框
- Xcode cannot run using the selected device-无法真机调试
- Service的肤浅了解
- Swift笔记.01
- 第12周—项目5 迷宫问题之图深度优先遍历解法
- TestFrameWork API接口测试框架
- SVN switch 用法详解
- 安卓上手-实现游戏2048
- break continue详解
- 三元组创建矩阵 一次定位快速转置 矩阵的加法、减法、乘法
- 黑马程序员 JavaSE-05 面向对象(上)