Service
来源:互联网 发布:wps h5是什么软件 编辑:程序博客网 时间:2024/06/06 09:50
1、两种启动、停止服务方式:
`
-startService()和stopService():停止之后,别的应用程序无法再访问此项目注册的服务。-bindService()和unbindService():停止之后,别的应用程序仍可继续访问此项目注册的服务。只有当服务从所有客服端解绑定后,系统才会将其销毁。
2、重要的回调方法:
-1)onStartCommand():调用startService()方法请求服务启动时,调用此方法。此方法一旦执行,就会无限执行(只是一直存在,并不类似于while语句一直在执行语句。只有当调用startService()方法时,此方法类的语句才会再次被执行,其它应用程序在服务停止之前调用startService()时也执行)。只有通过stopSelf()或()stopService()方法停止服务才会停止此方法的运行。-2)onBind():调用bindService()和服务绑定时,调用此方法。此方法返回一个IBinder,如果不想允许绑定,则让此方法返回null;如果运行绑定,则必须返回一个IBinder向客服端提供用来与服务通信的接口。-3)onCreate():服务第一次创建时调用且只调用一次,在onStartCommand()或onBind()之前调用。-4)onDestroy():服务不再使用,即将销毁时,调用此方法。
3、进程间通信使用AIDL:步骤如下
-1)在A项目的AA包下新建一个AIDL文件IMyService.aidl;-2)IMyService.sidl中的内容是一个接口类型,与JAVA定义接口类似;-3)在A项目AA包下新建一个Myservice类并继承Service类。接着在此类中创建一个内部类MyServiceImpl并继承IMyService.Stub(注意就是之前的AIDL文件),并复写IMyService.aidl里的方法-4)最重要的一步:在onBind()方法中返回一个IBinder对象--return new MyServiceImpl();。运行其他应用程序绑定服务,进而达到通信目的。-5)上一步提到在onBind()方法中返回IBinder对象,这就意味着程序只能通过bindService()启动并绑定服务。-6)Myservice完成并保存之后,会在项目A中的gen目录下的AA包下自动生成一个IMyService.java文件(与aidl同名)。
4、在项目中开启并绑定服务:这里主要是bindService()启动绑定服务。通过AIDL进程间通信。
-1)bindService(service, conn, flags); -service:Intent对象,action值为服务注册信息; -conn:ServiceConnectiond对象,在连接时初始化一个IMyService对象(IMyService myService = IMyService.Stub.asInterface(service);); -flags:取Context.XXX值。-2)本项目即A项目中直接使用bindService()绑定,应用IMyService对象调用MyServiceImpl(AIDL中方法)中方法。-3)在B项目即其他项目中绑定服务与在A项目中一样。但是一定要注意:将A中的gen目录下的AA包下自动生成一个IMyService.java文件连同AA包一起复制到B项目的src下。
5、咋A项目的配置文件中配置服务:
<service android:name=".Myservice"> <intent-filter > <action android:name="cm.example.android5module_service.Myservice"/> </intent-filter> </service>
6、A项目下的AA包下的MainActivity代码如下:
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.Handler;import android.os.IBinder;import android.os.Message;import android.os.RemoteException;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class MainActivity extends Activity { Button bt ; TextView tv; Intent intent; IMyService myService; int i=0; //连接服务器 ServiceConnection serviceConnection = new ServiceConnection(){ @Override public void onServiceConnected(ComponentName name, IBinder service) { myService = IMyService.Stub.asInterface(service); } @Override public void onServiceDisconnected(ComponentName name) { tv.setText("连接失败"); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt = (Button)this.findViewById(R.id.button); tv = (TextView)this.findViewById(R.id.textview); bt.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { //bindService()启动并绑定服务 if(i++==0) { bindService(new Intent("cm.example.android5module_service.Myservice"), serviceConnection, Context.BIND_AUTO_CREATE); } else { try { tv.append(myService.getValue()); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /* //startService()启动服务 intent = new Intent(); intent.setAction("cm.example.android5module_service.Myservice"); startService(intent); */ } }); /* Myservice.handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if(msg.what == 0x111){ tv.append(msg.obj.toString()); } } }; */ } @Override protected void onDestroy() { super.onDestroy(); unbindService(serviceConnection);// stopService(intent); }}
7、A项目下的AA包下的Myservice代码如下:
import android.app.Service;import android.content.Intent;import android.os.Handler;import android.os.IBinder;import android.os.Message;import android.os.RemoteException;import android.widget.Toast;public class Myservice extends Service { public static Handler handler; int i = 0; String str = "num:"; public static String bb = null; @Override public IBinder onBind(Intent intent) {// return null;//不允许绑定 return new MyServiceImpl();//绑定服务 } @Override public int onStartCommand(Intent intent, int flags, int startId) { Toast.makeText(getBaseContext(), bb, Toast.LENGTH_SHORT).show(); str += i+""; i++; Message msg = new Message(); msg.what = 0x111; msg.obj = str; handler.sendMessage(msg);//运行B项目时这行代码注释掉 return START_STICKY ; } //内部类,该类是IMyService.Stub的子类(自定义的AIDL文件) public class MyServiceImpl extends IMyService.Stub{ @Override public String getValue() throws RemoteException { // TODO Auto-generated method stub return "lalanilcsbkjnib"; } }}
8、A项目下的AA包下的IMyService.aidl代码如下:
package cm.example.android5module_service;interface IMyService { String getValue(); }
9、A项目下activity_main.xml代码如下:
<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" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button" android:text="读取service数据" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:id="@+id/textview" /></LinearLayout>
`
10、B项目下的MainActivity代码如下:
注意:将A中的gen目录下的AA包下自动生成一个IMyService.java文件连同AA包一起复制到B项目的src下。
`import cm.example.android5module_service.IMyService;import android.annotation.SuppressLint;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.os.RemoteException;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.webkit.WebChromeClient;import android.webkit.WebSettings;import android.webkit.WebView;import android.webkit.WebViewClient;import android.widget.Button;import android.widget.TextView;public class MainActivity extends Activity{ private IMyService myService = null; TextView tv; int i=0; private ServiceConnection serviceConnection = new ServiceConnection(){ @Override public void onServiceConnected(ComponentName name, IBinder service) { myService = IMyService.Stub.asInterface(service); } @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //测试AIDL tv = (TextView) findViewById(R.id.tv); Button bt = (Button)this.findViewById(R.id.bt); // bt.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { //bindService()启动并绑定服务 if(i++==0) { bindService(new Intent("cm.example.android5module_service.Myservice"), serviceConnection, Context.BIND_AUTO_CREATE); } else { try { tv.append(myService.getValue()); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /* //startService()启动服务 //将A项目中的Myservice类中的onStartCommand()方法中的handler.sendMessage(msg); 注释掉。 Intent intent = new Intent(); intent.setAction("cm.example.android5module_service.Myservice"); startService(intent); */ } }); } }
11、B项目下的activity_main.xml代码如下:
<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" tools:context="${relativePackage}.${activityClass}" android:orientation="vertical" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="连接" android:id="@+id/bt" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:id="@+id/tv" /> </LinearLayout>
`
0 0
- Service
- Service
- service
- Service
- service
- Service
- Service
- service
- Service
- Service
- Service
- service
- service
- Service
- Service
- Service
- Service
- Service
- 苹果App Store开发者帐户从申请,验证,到发布应用(3)
- android:exported 属性详解
- BellmanFord单源最短路径路径算法差分约束系统ZOJ–2770
- 苹果App Store开发者帐户从申请,验证,到发布应用(4)
- 【SQL】DBCC
- Service
- Linux GCC升级4.8.2 支持C++11 (CentOS)
- SVN --- 团队开发
- thinkphp相关积累
- UIWindow & UIWindowLevel笔记
- 关于新手学习心态的体会:聚焦
- OC面向对象
- 如何用JS弹出“有输入框并带有是、否、取消按钮”的对话框。
- iOS 系统相机的调用