Android realizes the function of vibrating prompt after phone _android

Source: Internet
Author: User
Tags readline system log

Some mobile phones will have vibration prompts after the phone is connected, which has the advantage of waiting to be connected to the ear to listen to, reduce radiation. This article will talk about how to implement the phone's vibrating cue function on an Android phone, mainly for outgoing calls.

the phone status provided by the Android SDK

Obviously, to have a vibrating cue when the phone is connected, you first need to know when the phone is connected. The Android SDK does not give you a way to read this state directly. Here are three phone statuses provided by the phone service class Telephonymanager of the Android SDK:

Call_state_idle Idle State

Call_state_offhook Pick Machine Status

Call_state_ringing Bell State

These states are easy to understand: Pick up the microphone (for the mobile phone), but this state may occur when the incoming call is connected, or when the call is made, but it is not possible to indicate that the outgoing call is connected. Through the above 3 states we can only combine the hanging machine and call to connect these two states. And the function we are going to achieve today is not possible.

It seems that we need to find other ways to achieve, the SDK is unreliable ah ...

Android Run log analysis

It's good that Android has a lot of log generated when it runs and see if we can find the wave port from there. We select the Android Radio module log to analyze. First we need to write a piece of code to read radio related log, read log will have to use the logcat.

 Process process; 
  InputStream InputStream; 
  BufferedReader BufferedReader; 
  try { 
   process = Runtime.getruntime (). EXEC ("Logcat-v time-b Radio"); 
   InputStream = Process.getinputstream (); 
   InputStreamReader InputStreamReader = new InputStreamReader ( 
     inputstream); 
   BufferedReader = new BufferedReader (inputstreamreader); 
 String str = ""; 
while (str = Bufferedreader.readline ())!= null) { 
  log.i ("Mlogcat", str); 
} 
catch (Exception e) { 
    
  

In addition, to allow the program to read the system log, you need to specify permissions to add content to the Androidmanifest.xml file.

xml/html Code

<uses-permission android:name= "Android.permission.READ_LOGS" ></uses-permission>

With this code we can output the radio log so that we can analyze the call process by looking at the logs in the DDMS. The specific captured log is not posted, you can write their own programs through the above code to crawl and analysis. I'll just say the results of my analysis.

Some clues were found by analyzing log. A few of these logs are useful:

Get_current_calls id=1,dialing

Get_current_calls id=1,alerting

Get_current_calls id=1,active

Because the log is longer I only take the first part of each log, the real will be much more content. When we dial out the phone, we enter a few logs.

Dial-> reminder-> activity

This is roughly the process. After several tests found that the phone is connected to enter the active state, and will output: Get_current_calls id=1,active This log, so far we have been close to success.

But then I found that when the dialing started to the phone to connect the time will pass many times the "Dial-> remind-> activity" such a state change, only when the microphone beep sounded get_current_calls This log will be locked in alerting. The Get_current_calls log will not appear until the phone is connected.

Perhaps the above statement is not very clear, in other words, before the call is connected to a number of get_current_calls ACTIVE such a log, and only once the phone was generated. This is causing us trouble. Can not simply crawl get_current_calls ACTIVE such information to judge.

We can only make it through some logical judgments.

Instance Code explanation

Here's a look at my code:

Class Testthread implements Runnable {//vibrator vibrator Mvibrator; 
 Telephone service Telephonymanager Telmanager; 
  Public Testthread (Vibrator Mvibrator, Telephonymanager telmanager) {this.mvibrator = Mvibrator; 
 This.telmanager = Telmanager; 
  @Override public void Run () {//get current phone status int callstate = Telmanager.getcallstate (); 
  LOG.I ("Testservice", "beginning ...") + Thread.CurrentThread (). GetName ()); 
  Record dialing start time long ThreadStart = System.currenttimemillis (); 
  Process process; 
  InputStream InputStream; 
  BufferedReader BufferedReader; 
   try {process = Runtime.getruntime (). EXEC ("Logcat-v time-b Radio"); 
   InputStream = Process.getinputstream (); 
   InputStreamReader InputStreamReader = new InputStreamReader (InputStream); 
   BufferedReader = new BufferedReader (InputStreamReader); 
   String str = ""; 
   Long Dialingstart = 0; 
   Boolean enablevibrator = false; 
   Boolean isalert = false; while (str = Bufferedreader.readline ())!= null) {
    If the status of the phone becomes idle from the pick machine, destroy the thread if (callstate = Telephonymanager.call_state_offhook && telmanager.getcallst 
    ate () = = Telephonymanager.call_state_idle) {break; 
    }//Thread runs 5 minutes automatically destroys if (System.currenttimemillis ()-ThreadStart > 300000) {break; 
    } log.i ("Testservice", Thread.CurrentThread (). GetName () + ":" + str); Record GSM status dialing if (Str.contains ("Get_current_calls") && str.contains ("dialing")) {//when Dialin G begins and has passed alerting or first dialing if (!isalert | | dialingstart = = 0) {//Record dialing state generation time Dialingstart = Sys 
      Tem.currenttimemillis (); 
     Isalert = false; 
    } continue; 
      
     } if (Str.contains ("Get_current_calls") && str.contains ("alerting") &&!enablevibrator) { 
     Long temp = System.currenttimemillis ()-Dialingstart; 
     Isalert = true; This is the key, when the first dialing state of the time, with the current alerting interval of more than 1.5 seconds and within 20 seconds//so that the next active state is connected to the call.
     if (temp > 1500 && Temp < 20000) {Enablevibrator = true; 
     LOG.I ("Testservice", "interval ..." + temp + "..." + thread.currentthread (). GetName ()); 
    } continue; } if (Str.contains ("Get_current_calls") && str.contains ("ACTIVE") && Enablevibrator) {m 
     Vibrator.vibrate (100); 
     Enablevibrator = false; 
    Break 
  } log.i ("Testservice", "End ...") + Thread.CurrentThread (). GetName ());  catch (Exception e) {//Todo:handle Exception}}}

My method is more far-fetched, is by judging the first time between dialing and each alerting interval, if the interval is greater than 1.5 seconds, then think has entered the "beep" of the time, then the next active will be the phone. This 1.5-second is obtained by analyzing the log. But I've always felt insecure about this approach. If you have a good way to communicate.

All that's left is to have this thread trigger when the phone dials out and be ready to stay on the phone. The service can be used with receiver to achieve. Service to achieve resident, receiver to achieve the monitoring of outgoing calls. Basically we can complete the function we want.

I have tested the above code, 99% effective, haha. This refers to some of the basics of Android, such as Logcat, Service, Receiver, if you do not understand the words can find relevant articles under the study.

Through this article hope to help the development of Android friends, thank you all for the support of this site!

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.