rshea0
rshea0

Reputation: 12249

Android detect incoming calls?

I am trying to develop an app where it will turn on your ringer if someone calls you a certain amount of times in a row in a certain period of time. This is my first real app, so I'm a little stuck.

How would I record whenever a call is received in an internal list? Would this need to be a service to always be running, or could this work in a normal app by just receiving the intent of the dialer app?

I apologize if this question is a little vague.

Upvotes: 1

Views: 2121

Answers (2)

sharma_kunal
sharma_kunal

Reputation: 2192

use single Tone Class for recording 


public class Recording {

    private static MediaRecorder recorder;
    private File audiofile;

    private static Recording mInstance;

    public MediaRecorder getRecorder() {
        System.out.println("From singleton..!!!");
        return recorder;
    }

    public static Recording getInstance(Context context) {
        return mInstance == null ? (mInstance = new Recording(context))
                : mInstance;
    }

    private Recording(Context context) {
        System.out.println("Again initiated object");
        File sampleDir = Environment.getExternalStorageDirectory();
        try {
            audiofile = File.createTempFile("" + new Date().getTime(), ".amr",
                    sampleDir);
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }

        recorder = new MediaRecorder();
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        recorder.setOutputFile(audiofile.getAbsolutePath());
    }
}

Upvotes: 0

Martin Cazares
Martin Cazares

Reputation: 13705

The best way to do it is, by declaring your broadcast receiver in the manifest, this will cause that the code on your BroadcastReceiver class to get executed everytime the event is fired, without the need of a service running in the background all the time, let the OS handle the observing part for you...

<receiver android:name=".ReceiverExample">
  <intent-filter>
    <action android:name="android.intent.action.PHONE_STATE" />
  </intent-filter>
</receiver>

Now, in your broadcastreceiver class "ReceiverExample", create a SharedPreference to store the number of incomming calls, and based on that, you can validate if is time to do something else or not...

  public class ReceiverExample extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
     //Logic to listen incoming calls, and keep track of them using Shared Preferences..
    }
  }

Services are good for long tasks but the OS it self is well suited to Monitor/Observe events (like Telephony events e.g. Incomming calls...), try not to re-do the OS work by creating Services just to monitor already known events...

Regards

Upvotes: 4

Related Questions