1

这是我的服务文件:

public class SMSCounterService extends Service {
    /** Called when the activity is first created. */
    @Override
    public void onCreate() {
        Log.d(LOG_TAG, "SMSCounterService: Constructor");
        super.onCreate();
        dbActivities = new DBActivities(this);
        dbActivities.open();    // Create or open the existing DB if already created

        IntentFilter filter = new IntentFilter();
        filter.addAction("android.provider.Telephony.SMS_SENT");
        registerReceiver(receiver, filter);
    }

    @Override
    public void onDestroy() {
        unregisterReceiver(receiver);
        dbActivities.close();
        super.onDestroy();
    }

    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals("android.provider.Telephony.SMS_SENT")) {
                smsCount++;
                Log.d(LOG_TAG, "SMSCounterService: smsCount=" +smsCount);
                long dbInsertReturn = dbActivities.insertIntoDB(smsCount);
            }
        }
    };

我可以看到日志“SMSCounterService:构造函数”。因此调用了 onCreate() 方法。但是当我发送短信时,BroadcastReceiver 无法识别它。

4

2 回答 2

0

确保您已在AndroidManifest.xml中添加所需的权限

发送:

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

对于接收:

<uses-permission android:name="android.permission.RECEIVE_SMS" />
于 2012-05-10T07:16:11.470 回答
0

AFAIK,没有像 android.provider.Telephony.SMS_SENT 这样的意图。我通过冰淇淋三明治做了“grep”,但找不到。

使用 ContentObserver 方法。注册您的观察者以收听更新消息提供者。(决定将其注册为答案,谁知道也许有人会接受它;))

于 2012-05-11T04:59:54.073 回答