Service phone bug in Android

Source: Internet
Author: User

Components-Services

The background of a long-running without interface of an activity.


Phone Wire:

Demand:

1. No interface, general users do not see, boot automatically start

2. Long-term background operation, monitor the current user's phone status

3. Call status, open a recorder, and then record the call records, saved in the SD card.


Steps to create a service:

1. Write a class that inherits the service components of the system

2. manifest file Configuration

3. The OnCreate () method in the service to do the operation of the service initialization

4. In the OnDestroy () method of the service do a service wrap-up operation

5. Turn on service StartService (intent) StopService (intent).


I. Write your own interface to control the opening and closing of a service

The XML file is simple, with the two button turned on and off the service, not given.


Mainactivity.java file,

Package Com.itheima.phonelistener;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);} /** * Open Service button corresponding Click time * @param view */public void Start (view view) {//define the Service intent object Intent Intent = new Intent (This,phonestatusser Vice.class); StartService (intent);} /** * Close the service button corresponding to the click Time * @param view */public void Stop (view view) {Intent Intent = new Intent (this,phonestatusservice.class); StopService (intent);}}

service Phonestatusservice.java file,

Package Com.itheima.phonelistener;import Java.io.file;import Java.io.ioexception;import android.app.Service;import Android.content.intent;import Android.media.mediarecorder;import Android.media.mediarecorder.audiosource;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 {public Static final String TAG = "Phonestatusservice";//declaring service class for mobile phone system state management private Telephonymanager tm;//the listener for the phone state Myphonelistener myphonelistener;//defines a telephone recorder private mediarecorder mediarecorder; @Overridepublic IBinder Onbind ( Intent arg0) {return null;} Called by the system if the service is first created//executed at the time the services were created. @Overridepublic void OnCreate () {//Get mobile phone status management services. TM = (Telephonymanager) getsystemservice (telephony_service);//Create a listener to listen for changes in phone call status. Myphonelistener = new Myphonelistener ();//start listening for the user's call status Tm.listen (Myphonelistener, Phonestatelistener.Listen_call_state); Super.oncreate ();} Called by the system to notify a service of it is no longer used and is being removed//when the service is stopped. @Overridepublic void OnDestroy () {//service stops cancelling the phone's listener tm.listen (Myphonelistener, Phonestatelistener.listen_none); Myphonelistener = Null;super.ondestroy ();} Private class Myphonelistener extends phonestatelistener{//executes the following code when the phone call status changes. State phone status//incomingnumber call number @overridepublic void oncallstatechanged (int state, String incomingnumber) {try {// Determine our current phone call status switch (state) {//Phone is idle, no one calls no 0-ring case telephonymanager.call_state_idle:log.i (TAG, "The phone is idle, Check whether the recorder is turned on, if it is on, save audio to sd card "), if (mediarecorder!=null) {//8. Stop recording Mediarecorder.stop ();//9. Release resources. Mediarecorder.release ();//convenient for garbage collector to reclaim resources Mediarecorder = null;//upload audio files to the server. Network Request API. Delete the files temporarily saved by the SD card. }break;//Mobile phone 0 State case TELEPHONYMANAGER.CALL_STATE_RINGING:LOG.I (TAG, "mobile phone ringing state, quietly open a tape recorder, ready to record.") //1. The first step is to create an instance of a multimedia recorder Mediarecorder = new Mediarecorder ();//2. Specifies that the audio source of a sound Recorder is a microphone mediarecorder.setaudiosource ( Audiosource.mic);3. Specify the format of this audio file. Mediarecorder.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP);//4. Specifies the file path saved by the recorded audio file. Environment.getexternalstoragedirectory (), incomingnumber+ ". 3gp"); Mediarecorder.setoutputfile ( File.getabsolutepath ());//5. Sets the encoding format for audio Mediarecorder.setaudioencoder (MediaRecorder.AudioEncoder.DEFAULT);//6. Ready to start recording mediarecorder.prepare (); break;//phone on status, user is calling case telephonymanager.call_state_offhook:log.i (TAG, " The phone is on a call and starts recording to record what the user said. "), if (mediarecorder!=null) {//7. Start recording Mediarecorder.start ();} break;} Super.oncallstatechanged (state, incomingnumber);} catch (Exception e) {e.printstacktrace ();}}}}

configuration file Androidmanifest.xml add

<service android:name= "Com.itheima.phonelistener.PhoneStatusService" >        </service>
<!--monitoring the status of a user's phone violates the user's privacy, requires permissions--    <uses-permission android:name= "Android.permission.READ_PHONE_STATE"/ >    <!--recording requires permission--    <uses-permission android:name= "Android.permission.RECORD_AUDIO"/>    <!--write SD card--    <uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>


Two. Start-up service automatically

Configuration file Androidmanifest.xml Add

<!--    receive the phone to start the broadcast event permissions, in the previous version of 4.0, do not need to add this permission can also turn on startup,    after the 4.0 version does not include 4. 0 You must add power-on boot permissions--    <uses-permission android:name= "Android.permission.RECEIVE_BOOT_COMPLETED"/>


Custom boot broadcast recipients,

Package Com.itheima.phonelistener;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import Android.util.log;public class Bootcompletereceiver extends Broadcastreceiver {private Static final String TAG = "Bootcompletereceiver"; @Overridepublic void OnReceive (context context, Intent Intent) {log.i (TA G, "Mobile phone has been started, monitoring the launch of mobile phone broadcast events, the background monitoring Services"); Intent i = new Intent (context,phonestatusservice.class); Context.startservice ( i);}}

Check the SD card path to find the. 3gp file, which is the recording file for the call.

Service phone bug in Android

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.