0

我想创建一个应用程序来监控用户呼叫(呼出/呼入)和短信(呼入)。

所以我创建了 3 个广播接收器:

来电接收者

public class IncomingCallReceiver extends BroadcastReceiver 
{
@Override
public void onReceive(Context context, Intent intent)
{
    MyPhoneStateListener phoneListener = new MyPhoneStateListener(context);
    TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
    telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}

}

去电接听者

public class OutgoingCallReceiver extends BroadcastReceiver 
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
         Log.d("DEBUG", "RINGING (outgoing call) with number: " + number);
         Toast.makeText(context, "Outgoing call number: " + number, Toast.LENGTH_LONG).show();
    }
}

传入短信接收者

public class IncomingSmsReceiver extends BroadcastReceiver 
{
    final SmsManager sms = SmsManager.getDefault();

    @Override
    public void onReceive(Context context, Intent intent) {
    // Retrieves a map of extended data from the intent.
    final Bundle bundle = intent.getExtras();

    try 
    {
        if (bundle != null) 
        {
            final Object[] pdusObj = (Object[]) bundle.get("pdus");
            for (int i = 0; i < pdusObj.length; i++) 
            {
                SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                String phoneNumber = currentMessage.getDisplayOriginatingAddress();
                String senderNum = phoneNumber;
                String message = currentMessage.getDisplayMessageBody();

                Log.i("SmsReceiver", "senderNum: " + senderNum + "; message: " + message);
                Toast.makeText(context, "senderNum: " + senderNum + ", message: " + message, Toast.LENGTH_LONG).show();
            }
        } 
        // bundle is null
    } 
    catch (Exception e) 
    {
        Log.e("SmsReceiver", "Exception smsReceiver" + e);
    }
}
}

并将它们全部定义为我的清单文件:

   <receiver android:name=".IncomingSmsReceiver" >
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>

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

    <receiver android:name=".OutgoingCallReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.ACTION_NEW_OUTGOING_CALL" />
        </intent-filter>
    </receiver>

这一切都很好,但现在我有一些问题,我需要一些指示:

1.如果我在清单文件中定义了BroadcastReceivers这种方式,那么如果我错了,请纠正我,但如果我的应用程序被关闭/杀死,那么我将不会继续监视这些操作,对吗?

2.是什么让我认为我需要创建一个Service可以运行/注册这些的BroadcastReceivers,所以我创建了以下内容Service

CallAndSMSDetectService

public class CallAndSMSDetectService extends Service {

IncomingCallReceiver incomingCallReceiver;
OutgoingCallReceiver outgoingCallReceiver;
IncomingSmsReceiver incomingSmsReceiver;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    int res = super.onStartCommand(intent, flags, startId);

    outgoingCallReceiver = new OutgoingCallReceiver();
    incomingCallReceiver = new IncomingCallReceiver();
    incomingSmsReceiver = new IncomingSmsReceiver();

    IntentFilter outgoingCallIntentFilter = new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL);
    registerReceiver(outgoingCallReceiver, outgoingCallIntentFilter);

    return res;
}

@Override
public void onDestroy() {
    super.onDestroy();

    unregisterReceiver(outgoingCallReceiver);
}

@Override
public IBinder onBind(Intent intent) {
    // not supporting binding
    return null;
}
}

我的问题是我找不到合适的 IntentFilterSMS_RECEIVED和 for PHONE_STATE,所以我不知道如何像使用Intent.ACTION_NEW_OUTGOING_CALL.

如果有人能在这个问题上指引我正确的方向,我会很高兴?以及如何正确完成。

谢谢。

4

2 回答 2

0

事实上,你是完全正确的。当您的应用程序启动时,您需要创建服务。无需创建实例

传入呼叫接收器传入呼叫接收器;

OutgoingCallReceiver 传出CallReceiver;

传入SmsReceiver 传入SmsReceiver;

尝试为接收者设置清单优先级。

<receiver android:name=".OutgoingCallReceiver">
            <intent-filter android:priority="2147483647">
                <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
            </intent-filter>
</receiver>

希望我对你有所帮助。如果你有问题 - 问我。

于 2013-10-01T10:49:18.840 回答
0

您可以创建一个粘性服务以保持广播可用。这样,即使您关闭或杀死了您的应用程序,您也可以确保您的广播始终在运行。

于 2013-10-01T10:55:42.190 回答