简体   繁体   English

Android检测来电?

[英]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. 如果这个问题有点含糊,我道歉。

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... 最好的方法是,通过在清单中声明广播接收器,这将导致每次触发事件时,BroadcastReceiver类上的代码都会被执行,而不需要在后台运行服务,让操作系统为您处理观察部分......

<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... 现在,在您的broadcastreceiver类“ReceiverExample”中,创建一个SharedPreference来存储进入呼叫的数量,并根据这个,您可以验证是否有时间做其他事情......

  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 eg Incomming calls...), try not to re-do the OS work by creating Services just to monitor already known events... 服务适用于长期任务,但其自身的操作系统非常适合监视/观察事件(如电话事件,例如Incomming调用...),尽量不要通过创建服务来监视已知事件来重新执行操作系统工作。 ..

Regards 问候

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());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM