77,78.PhoneListening
public class MyService extends Service {
private final String TAG = "MyService";
private TelephonyManager tm;
private MyPhoneStateListener listener;
public MyService() {
}
@Override
public void onDestroy() {
Log.d(TAG, "onDestroy");
super.onDestroy();
tm.listen(listener, PhoneStateListener.LISTEN_NONE);
}
@Override
public void onCreate() {
super.onCreate();
tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
listener = new MyPhoneStateListener();
tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
private class MyPhoneStateListener extends PhoneStateListener{
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state){
case TelephonyManager.CALL_STATE_IDLE:
Log.d(TAG, "电话闲置");
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.d(TAG, "电话响铃");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d(TAG, "电话接听");
break;
}
}
}
79,80.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.localselectperson.MainActivity">
<Button
android:id="@+id/btn_1"
android:textSize="20sp"
android:onClick="onStartService"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="绑定服务"/>
<Button
android:id="@+id/btn_11"
android:textSize="20sp"
android:onClick="onStopService"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="解绑服务"/>
<EditText
android:id="@+id/edit_query"
android:hint="在此输入您的ID"
android:textSize="20sp"
android:inputType="number"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_3"
android:textSize="20sp"
android:onClick="onSelect"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查询信息"/>
<TextView
android:id="@+id/show_text"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>