2

我正在制作一个需要在 Android 手机上有来电时运行的应用程序。我希望我的应用程序只听来电并Activity在后台运行它自己的。实际上,我正在制作一个应用程序,它的工作原理就像有来电时,然后Arduino 板上的LED闪烁。

4

4 回答 4

2

I think Chapter 12. Telephone Applications in Android Cookbook should prove helpful:

The short version, however, is that you need to listen to a broadcast message indicating that the phone state has change. To do that, you subclass BroadcastReceiver and add some code in your manifest to capture the event.

File AndroidManifest.xml:

<application android:icon="@drawable/icon" android:label="Incoming Call Interceptor">

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

</application>

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

File IncomingCallInterceptor.java:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public class IncomingCallInterceptor extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        String msg = "Phone state changed to " + state;

        if (TelephonyManager.EXTRA_STATE_RINGING.equals(state)) {
            String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
            msg += ". Incoming number is " + incomingNumber;

            // TODO This would be a good place to "Do something when the phone rings" ;-)
        }
        Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
    }
}

The page I linked to has more information about how this actually works and additional things you could detect (like when the user answers or rejects the call). So make sure to read it.

于 2012-07-20T11:44:34.660 回答
1

当电话响起时做一些事情解释了如何开始听电话响起,然后你需要启动一个服务

于 2012-07-20T11:41:55.197 回答
0

您可以为此设置一个意图过滤器。意图将绑定到一个活动。

这是您必须在清单中添加的内容(在活动中):

<intent-filter> 
            <action android:name="android.intent.action.CALL" />

据我所知,这适用于拨出电话,您必须检查它是否也适用于来电..

于 2012-07-20T11:46:23.447 回答
0

如果你想使用蓝牙来做到这一点,那么看看一个名为Amarino的项目,你可以在网上安装一个便宜的 6 美元蓝牙模块。

我这样做了,效果很好。

于 2012-07-25T14:00:16.153 回答