Android game call handling

Source: Internet
Author: User

Call thread type

If there is a phone call, the blue code will be executed whether the phone is called or the phone is called in.

Telephonymanager. call_state_ringing: Incoming call status

Telephonymanager. call_state_offhook

Telephonymanager. call_state_idle: The status is monitored whether the phone is called or when the phone is called.

The Answer call will listen to three statuses, and the call will only listen to the following two. Note:

The pause of the main thread, wait (); the execution is determined by the Boolean value. Thread wake up by Y ();


/**
* Telephone listener
*
* @ Author yuan
*
*/
Public class callreceiver extends broadcastreceiver {

Private string tag = "callcycler ";
// Falg with a telephone sign
Static Boolean incomingflag = false;
// Falg with a phone exit sign
Static Boolean outflag = false;
// Falg
Public static Boolean waitflag = false;
// Do not clear the image flag
Public static Boolean pause = false;
String incoming_number = NULL;

@ Override
Public void onreceive (context, intent ){
Util. printlog (TAG, "listener phone number ");
Telephonymanager TM = (telephonymanager) Context
. Getsystemservice (service. telephony_service );
Waitflag = true;
If (basecontrol. msoundplayermanager! = NULL ){
Basecontrol. msoundplayermanager. setopensound (false );
}
Switch (TM. getcallstate ()){
Case telephonymanager. call_state_ringing:
Incomingflag = true;
Incoming_number = intent. getstringextra ("incoming_number ");
Util. printlog (TAG, "ringing:" + incoming_number );
Break;
Case telephonymanager. call_state_offhook:
Outflag = true;
If (incomingflag ){
Util. printlog (TAG, "Answer:" + incoming_number );
}
Break;
Case telephonymanager. call_state_idle:
If (incomingflag | outflag ){
Waitflag = false;
Incomingflag = false;
Outflag = false;
If (basecontrol. msoundplayermanager! = NULL ){
Basecontrol. msoundplayermanager
. Setopensound (basecontrol. isplaysound );
}
Try {
Synchronized (basecontrol. mgamesurfaceview. mytimerthread ){
Basecontrol. mgamesurfaceview. mytimerthread. Policy (); // start the thread
}
} Catch (exception e ){
E. printstacktrace ();
}
}
Break;
}
}

}

Public class timerthread extends thread {

/**
* Thread startup
*/
@ Override
Public void run (){
Super. Run ();
While (timerflag ){
If (callcycler. waitflag ){
Synchronized (this ){
Try {
Wait ();
} Catch (interruptedexception e ){
E. printstacktrace ();
}
}
}
Long nowtime = system. currenttimemillis ();
If (chuzzlestate = chuzzleconst. gs_start ){
// Use the last pause time to calculate the progress
If (isusepausetime ){
Nowtime = pausegametime;
Isusepausetime = false;
}
If (! Basecontrol. isonlinemode ){
// 1. check whether a second has elapsed and update the progress bar
If (nowtime-startgametime)> chuzzleconst. decress_score_current_time ){
// 2. Determine whether to draw a settlement
If (currentscore <-50 | currentscore> 50 ){
Timerflag = false;
Chuzzlestate = chuzzleconst. gs_end_waitting;
Updateprogress ();

Message MSG = basecontrol. mhandler. obtainmessage (chuzzleconst. msg_game_msg_update_recorde, 0, 0, null );
Basecontrol. mhandler. sendmessage (MSG );

Drawbalance ();
Return;
}
// The progress is automatically reduced by 1%
Currentscore --;
// Update the progress bar
Updateprogress ();
Startgametime = nowtime;
}

}
// 2. Determine if the position has passed 10 s and prompt to move
If (nowtime-startsteptime)> chuzzleconst. help_overtime
&&! Istoucheddown &&! Isbusy ){
// Draw the prompt position
Drawpointout ();
// Get the current display time and cancel the painting prompt
Startsteptime = system. currenttimemillis ();
}
}

}
}

}

Add permission in androidmanifest. xml

<! -- Yuan -->
<Uses-Permission Android: Name = "android. Permission. wake_lock"/>
<! -- Add the permission to listen to the phone status -->
<Uses-Permission Android: Name = "android. Permission. read_phone_state"/>
<! -- Add the permission to make a phone call -->
<Uses-Permission Android: Name = "android. Permission. process_outgoing_cils"/>
<! -- Add the call permission -->
<Uses-Permission id = "android. Permission. call_phone"/>
<! -- Add the permission to receive text messages -->
<Uses-Permission Android: Name = "android. Permission. receive_sms"/>
<! -- Enable access to the inter network -->
<Uses-Permission Android: Name = "android. Permission. Internet"/>
<! -- Add the permission to obtain mobile phone information -->
<Uses-Permission Android: Name = "android. Permission. read_phone_state"/>
<! -- Add the permission to send text messages -->
<Uses-Permission Android: Name = "android. Permission. send_sms"/>
<! -- Add and modify the APN network permissions -->
<Uses-Permission Android: Name = "android. Permission. write_apn_settings"/>
<! -- Add to get wireless network permissions -->
<Uses-Permission Android: Name = "android. Permission. access_wifi_state"/>
<! -- Add Network Access Permissions -->
<Uses-Permission Android: Name = "android. Permission. access_network_state"/>
<! -- Add and modify Network permissions -->
<Uses-Permission Android: Name = "android. Permission. change_network_state"/>
<! -- Close the program permission -->
<Uses-Permission Android: Name = "android. Permission. restart_packages"/>
<! -- SD file write permission -->
<Uses-Permission Android: Name = "android. Permission. write_external_storage"/>

<Cycler Android: Name = "callcycler">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. new_outgoing_call"> </Action>
<Category Android: Name = "android. Intent. Category. Default"> </Category>
</Intent-filter>
<Intent-filter>
<Action Android: Name = "android. Intent. Action. phone_state"> </Action>
<Category Android: Name = "android. Intent. Category. Default"> </Category>
</Intent-filter>
<Intent-filter Android: Priority = "20">
<Action Android: Name = "android. provider. telephony. sms_received"/>
</Intent-filter>
</Cycler>

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.