关于BroadcastReceivers的几个问题:

时间:2013-10-01 10:30:55

标签: android broadcastreceiver android-service

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

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

IncomingCallReceiver

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

}

OutgoingCallReceiver

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

IncomingSmsReceiver

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

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

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

我的问题是我无法为SMS_RECEIVEDPHONE_STATE找到合适的IntentFilters,所以我不知道如何以编程方式注册它们,就像我对{{1}所做的那样}。

如果有人能指引我朝这个方向迈进正确的方向,我会很高兴吗?以及如何正确地完成它。

感谢。

2 个答案:

答案 0 :(得分:0)

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

的实例

IncomingCallReceiver incomingCallReceiver;

OutgoingCallReceiver outgoingCallReceiver;

IncomingSmsReceiver incomingSmsReceiver;

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

<receiver android:name=".OutgoingCallReceiver">
            <intent-filter android:priority="2147483647">
                <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
            </intent-filter>
</receiver>
希望我帮助过你。如果您有疑问 - 请问我。

答案 1 :(得分:0)

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