我有SMSReceiver类扩展BroadcastReceiver
。如何在后台使用registerReceiver()
和unregisterReceiver()
来发送批量短信。
我希望当关闭应用程序运行SMSReceiver以获取交付并将交付保存到数据库时。
public class SMSReceiver extends BroadcastReceiver {
private Realm realm;
private static final String ACTION_SMS_DELIVERED = "android.provider.Telephony.SMS_RECEIVED";
private String number, name;
@Override
public void onReceive(Context context, Intent intent) {
number = intent.getStringExtra("number");
name = intent.getStringExtra("name");
if (ACTION_SMS_DELIVERED.equals(intent.getAction())) {
switch (getResultCode()) {
case Activity.RESULT_OK:
try {
realm = Realm.getDefaultInstance();
GroupsModel toEdit = realm.where(GroupsModel.class)
.equalTo("GroupName", name)
.equalTo("Tell", number)
.equalTo("Status", false).findFirst();
if (toEdit.getStatus() == false) {
realm.beginTransaction();
toEdit.setStatus(true);
realm.commitTransaction();
}
realm.close();
// Toast.makeText(context, name + number,
// Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Log.d("a", e.toString());
}
break;
case Activity.RESULT_CANCELED:
Toast.makeText(context, "error",
Toast.LENGTH_SHORT).show();
break;
}
}
}