Android(安卓)服务入门(电话监听)
16lz
2021-01-26
<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: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=".MainActivity" > <Button android:onClick="Stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Stop" /> <Button android:onClick="Start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start" /></LinearLayout>
package com.example.listencall;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.Menu;import android.view.View;public class MainActivity extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Intent intent = new Intent(this, PhoneStatusService.class);startService(intent);}@Overridepublic boolean onCreateOptionsMenu(Menu menu){// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}public void Stop(View v){// 服务生命周期// creat startCommand start destory// 关闭生命周期Intent intent = new Intent(this, PhoneStatusService.class);stopService(intent);}public void Start(View v){// 服务生命周期// creat startCommand start destory//多次开启如果没有Destory则执行startCommand start// 关闭生命周期Intent intent = new Intent(this, PhoneStatusService.class);startService(intent);}}
package com.example.listencall;import java.io.IOException;import android.app.Service;import android.content.Intent;import android.media.MediaRecorder;import android.os.Environment;import android.os.IBinder;import android.telephony.PhoneStateListener;import android.telephony.TelephonyManager;import android.util.Log;public class PhoneStatusService extends Service{// 长期后台运行 如果用户不终止 则持续运行@Overridepublic void onCreate(){super.onCreate();Log.i("Service", "服务开启了……");// 监视电话状态// 电话管理器/服务TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);// 监听手机通话状态tm.listen(new MyPhoneStatusLister(), PhoneStateListener.LISTEN_CALL_STATE);}private class MyPhoneStatusLister extends PhoneStateListener{private MediaRecorder recorder;@Overridepublic void onCallStateChanged(int state, String incomingNumber){try{switch (state){case TelephonyManager.CALL_STATE_IDLE:// 空闲状态 无通话 无响铃if (null != recorder){recorder.stop();recorder.reset(); // You can reuse the object by goingrecorder.release(); // Now the object cannot be reusedSystem.out.println("录音结束……");}break;case TelephonyManager.CALL_STATE_RINGING:// 响铃状态// 创建录音机recorder = new MediaRecorder();System.out.println("来电号码:" + incomingNumber + "录音准备完毕……");recorder.setAudioSource(MediaRecorder.AudioSource.MIC);recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);recorder.setOutputFile("/sdcard/" + incomingNumber + ".aac");recorder.prepare();break;case TelephonyManager.CALL_STATE_OFFHOOK:// 通话状态if (null != recorder){System.out.println("正在录音……");recorder.start(); // Recording is now started}break;default:break;}} catch (IllegalStateException e){e.printStackTrace();} catch (IOException e){e.printStackTrace();}super.onCallStateChanged(state, incomingNumber);}}@Overridepublic void onDestroy(){super.onDestroy();Log.i("Service", "服务销毁了……");}@Overridepublic IBinder onBind(Intent arg0){// TODO Auto-generated method stubreturn null;}@Override@Deprecatedpublic void onStart(Intent intent, int startId){// TODO Auto-generated method stubLog.i("Service", "服务Start……");super.onStart(intent, startId);}@Overridepublic int onStartCommand(Intent intent, int flags, int startId){// TODO Auto-generated method stubLog.i("Service", "服务onStartCommand……");return super.onStartCommand(intent, flags, startId);}}
更多相关文章
- Android(安卓)服务端将位置信息发送给客户端的实现
- 修改状态栏颜色和状态栏字体颜色2
- Android(安卓)webview加载本地html实现跨域访问
- 10.Binder进阶:系统服务中的Binder
- Android启动过程 && Activity的生命周期 && Activity的开机自启
- Lifecycle-Aware Components生命周期组件 Lifecycle、LiveData
- android 横竖屏切换,activity的生命周期
- Android(安卓)sdk更新过程中出现问题 (连接不上Google的服务资源
- Android获取手机屏幕宽高、状态栏高度以及字符串宽高信息的方法