服務:在后台長期運行的沒有界面的組件
新建一個類PhoneService類,繼承系統的Service類
清單文件中 進行配置
新建一個節點<service>,設置名稱android:name=”.PhoneService”
類里面有幾個重要方法
onCreate()方法,服務被創建的時候調用
onDestory()方法,服務被銷毀的時候調用
開啟服務
獲取intent對象,new Intent(this,PhoneService.class),參數:上下文,字節碼
調用上下文對象的startService(intent),參數:intent對象
在服務的onCreate()方法里,執行一些長期操作
獲取TelephoneyManager對象,調用getSystemService(TELEPHONY_SERVICE)方法
調用TelephoneyManager對象的listen(istener,events)方法,監聽手機通話狀態,參數:
PhoneStateListener對象,使用內部類類繼承一下,要重寫一些方法
PhoneStateListener.LISTEN_CALL_STATE
新建一個內部類MyPhoneStateListener繼承PhoneStateListener,
重寫方法onCallStateChanged(state,incomingNumber),當手機的電話狀態變化的時候,回調此函數
在上面方法里面,switch判斷一下通話狀態,有以下三種TelephonyManager.CALL_STATE_IDLE空閑狀態,TelephonyManager.CALL_STATE_RINGING響鈴狀態,
TelephonyManager.CALL_STATE_OFFHOOK通話狀態
需要權限android.permission.READ_PHONE_STATE
MainActivity.java
package com.tsh.listentel; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //開啟服務 Intent intent=new Intent(this,PhoneService.class); startService(intent); } }
PhoneService.java
package com.tsh.listentel; import android.app.Service; import android.content.Intent; 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 return null; } //服務創建 @Override public void onCreate() { super.onCreate(); System.out.println("服務創建"); TelephonyManager tm=(TelephonyManager) getSystemService(TELEPHONY_SERVICE); tm.listen(new MyPhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE); } //內部類 private class MyPhoneStateListener extends PhoneStateListener{ @Override public void onCallStateChanged(int state, String incomingNumber) { super.onCallStateChanged(state, incomingNumber); switch (state) { case TelephonyManager.CALL_STATE_IDLE: System.out.println("空閑狀態"); break; case TelephonyManager.CALL_STATE_RINGING: System.out.println("響鈴狀態"); break; case TelephonyManager.CALL_STATE_OFFHOOK: System.out.println("通話狀態"); break; default: break; } } } //服務銷毀 @Override public void onDestroy() { System.out.println("服務銷毀"); super.onDestroy(); } }
Manifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.tsh.listentel" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="23" /> <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=".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"></service> </application> </manifest>