android学习之Service的笔记,里面service里有监听用户通话状态的实例
一、学习Service首先了解一下android系统的进程:
1、进程和线程之间的关系:一个进程里面可以有多个线程.进程如果挂了, 线程就没了。如果我们激活另外一个应用程序的activity肯定另外一个应用程序 所在的进程也会被创建出来
2、广播接收者接到广播后运行的周期很短,它是运行在主线程中的不能操作耗时的操作,这时候我们就应该调用service,service这个组件会长期的在后台运一般情况下不会别操作系统回收。
3、进程的优先级:
(1)Foreground process 前台进程 :优先级别最高,即便系统内存不足的时候 也不会杀死前台进程
(2)Visible process 可见进程 :优先级稍为低一点,如一些程序是小窗口的activity,总是可见的,这个小窗体后面的还可以看到的进程就是可见进程。
(3)Service process 服务进程: 存活时间比较长 .里面的子线程不会回收.
(4)Background process 后台进程:我们点开一个应用程序,然后点击home键返回到桌面,(没有退出)这个程序就是后台进程。
(5)Empty process 空进程: 没有任何的组件进程,就是进入进程后点击返回键直到退出,这个进程没有线程即activity。
二、service实例。其中完成了电话的状态监听服务。
(1)服务需要在清单文件里配置
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.servicephone" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.servicephone.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=".PhoneService" > <intent-filter > <action android:name="com.example.servicephone.Phoneservice"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </service> </application> </manifest>
(2)启动service有2中方法:其中在代码注释中有详细介绍:
package com.example.servicephone; import com.example.servicephone.PhoneService.myBinder; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support.v4.app.Fragment; 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.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.Button; import android.os.Build; public class MainActivity extends Activity implements OnClickListener{ private Button b1,b2,b3,b4; private ServiceConnection conn; private Intent intent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b1=(Button) findViewById(R.id.button1); b2=(Button) findViewById(R.id.button2); b3=(Button) findViewById(R.id.button3); b4=(Button) findViewById(R.id.button4); b1.setOnClickListener(this); b2.setOnClickListener(this); b3.setOnClickListener(this); b4.setOnClickListener(this); conn=new Myconnect(); intent=new Intent("com.example.servicephone.Phoneservice"); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button1: // System.out.println("clicked b1"); startService(intent); //直接启动,启动后服务与应用无关了,退出activity后服务仍旧运行。 break; case R.id.button2: // System.out.println("clicked b2"); stopService(intent); //直接关闭服务 break; case R.id.button3: // System.out.println("clicked b3"); bindService(intent, conn, Context.BIND_AUTO_CREATE);//绑定启动服务,activity被回收了,则服务也停止 break; case R.id.button4: // System.out.println("clicked b4"); unbindService(conn); break; default: break; } } class Myconnect implements ServiceConnection { @Override public void onServiceConnected(ComponentName name, IBinder service) { //这个函数在连接成功后,服务里onbindle()方法返回后执行,若返回是null则不执行,通过service参数可以调用服务的方法 //直接binder来调用 myBinder mc=(myBinder) service; mc.callmethod(); //用接口调用,其实没必要,用借口主要是可以共享服务的方法给其他的应用程序,即进程间的通信,下一篇就会讲到 Iservice is=(Iservice) service; is.callmethod(); } @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub } } @Override protected void onDestroy() { //如果用的是绑定启动服务的话,可以在这个应用程序结束的时候,解除绑定结束服务,以免服务异常终止。 super.onDestroy(); unbindService(conn); } }
(3)服务代码,即几个方法执行的时间
package com.example.servicephone; import android.app.Service; import android.content.Intent; import android.net.sip.SipRegistrationListener; import android.os.Binder; import android.os.IBinder; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; public class PhoneService extends Service { @Override public IBinder onBind(Intent intent) { //在使用绑定启动服务的时候执行 // TODO Auto-generated method stub System.out.println("onbind"); return new myBinder(); } @Override public int onStartCommand(Intent intent, int flags, int startId) {// 代替了onstart方法,为了兼容内部调用了start方法,在oncreate()执行后,启动服务后点击开启服务会再次执行 // TODO Auto-generated method stub return super.onStartCommand(intent, flags, startId); } public class myBinder extends Binder implements Iservice //Iservice是自定义的一个借口抛出服务里面的方法,在同一个应用程序中可以直接继承Bundle,在上个程序里的serviceConnection接口中返回的se //rvice里调用,上面用接口和Binder都实现了,接口主要还是用于进程之间通信。 { @Override public void callmethod() { Sayhello(); } } @Override public boolean onUnbind(Intent intent) { //解除绑定的时候 // TODO Auto-generated method stub System.out.println("onunbind"); return super.onUnbind(intent); } @Override public void onCreate() { //在服务启动后就执行,若存在服务,就不会执行这个方法 // TODO Auto-generated method stub System.out.println("oncreate"); TelephonyManager tm=(TelephonyManager) getSystemService(TELEPHONY_SERVICE); tm.listen(new Mylistener(), PhoneStateListener.LISTEN_CALL_STATE); //这是电话状态的监听器实例 super.onCreate(); } class Mylistener extends PhoneStateListener { @Override public void onCallStateChanged(int state, String incomingNumber) {//state 电话的状态,incomingNumber是来电的电话号码 // TODO Auto-generated method stub switch (state) { case TelephonyManager.CALL_STATE_IDLE: //空闲状态 System.out.println("it is free!"); break; case TelephonyManager.CALL_STATE_RINGING: // 铃响状态 System.out.println("it is ringing!"); break; case TelephonyManager.CALL_STATE_OFFHOOK: System.out.println("it is speaking!"); // 通话中的状态 break; default: break; } super.onCallStateChanged(state, incomingNumber); } } @Override public void onDestroy() { //服务结束时候执行 System.out.println("destroyed!!!!!!"); super.onDestroy(); } public void Sayhello() { System.out.println("hello service!"); } }
(4)接口定义
package com.example.servicephone; public interface Iservice { public void callmethod(); }