[Android] uses services to perform long-term background operations

Source: Internet
Author: User

Services: Non-interface components that run in the background for a long time

Create a new class Phoneservice class that inherits the Service class of the system

Configuration in the manifest file

Create a new node <service>, set the name android:name=. Phoneservice "

There are several important methods within the class

OnCreate () method, called when the service is created

Ondestory () method, called when the service is destroyed

Open service

Get Intent object,new Intent (This,phoneservice.class), Parameters: Context, byte code

Call the context object's StartService (intent), parameter:Intent Object

In the onCreate () method of the service , perform some long-term operations

Gets the telephoneymanager object, calling the getsystemservice (telephony_service) method

Call the Telephoneymanager object's listen (Istener,events) method, monitor the phone call status, parameters:

Phonestatelistener object, inherit it using the inner class class, to override some methods

Phonestatelistener.listen_call_state

Create a new inner class Myphonestatelistener inherit phonestatelistener,

Override method oncallstatechanged (state,incomingnumber), callback this function when the phone's phone status changes

In the above method,switch to determine the call status, there are the following three kinds of telephonymanager.call_state_idle idle state, Telephonymanager.call_state_ringing Bell State,

Telephonymanager.call_state_offhook Call Status

Requires permission Android.permission.READ_PHONE_STATE

Mainactivity.java

 PackageCom.tsh.listentel;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); //Open ServiceIntent intent=NewIntent ( This, Phoneservice.class);    StartService (Intent); }}

Phoneservice.java

 PackageCom.tsh.listentel;ImportAndroid.app.Service;Importandroid.content.Intent;ImportAndroid.os.IBinder;ImportAndroid.telephony.PhoneStateListener;ImportAndroid.telephony.TelephonyManager; Public classPhoneserviceextendsService {@Override Publicibinder onbind (Intent Intent) {//TODO auto-generated Method Stub        return NULL; }    //Service Creation@Override Public voidonCreate () {Super. OnCreate (); System.out.println ("Service Creation"); Telephonymanager TM=(Telephonymanager) Getsystemservice (Telephony_service); Tm.listen (NewMyphonestatelistener (), phonestatelistener.listen_call_state); }    //Inner class    Private classMyphonestatelistenerextendsphonestatelistener{@Override Public voidOncallstatechanged (intState , String Incomingnumber) {            Super. oncallstatechanged (State, Incomingnumber); Switch(state) { CaseTelephonyManager.CALL_STATE_IDLE:System.out.println ("Idle State");  Break;  CaseTelephonyManager.CALL_STATE_RINGING:System.out.println ("Ringing status");  Break;  CaseTelephonyManager.CALL_STATE_OFFHOOK:System.out.println ("Call status");  Break; default:                 Break; }        }            }    //Service Destruction@Override Public voidOnDestroy () {System.out.println ("Service Destruction"); Super. OnDestroy (); }}

Manifest.xml

<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.tsh.listentel"Android:versioncode= "1"Android:versionname= "1.0" >    <USES-SDKandroid:minsdkversion= "8"android:targetsdkversion= "All" />    <uses-permissionAndroid:name= "Android.permission.READ_PHONE_STATE"/>    <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" >        <ActivityAndroid:name=". Mainactivity "Android:label= "@string/app_name" >            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>        <ServiceAndroid:name=". Phoneservice "></Service>    </Application></Manifest>

[Android] uses services to perform long-term background operations

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.