这是我的服务文件:
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 无法识别它。