我正在使用广播接收器来拦截传入的消息。我已将意图数据从 OnReceive 方法传递给 WorkManager 类。我想从 pdu 中获取一个 SmsMessage,但是由于在 doWork 方法上接收到的数据是字符串,即使 pdu 对象也是一个字符串,因此很难从这样的 PDU 创建一个 SmsMessage 对象。下面是我的广播接收器代码;
if (Telephony.Sms.Intents.SMS_RECEIVED_ACTION.equals(intent.getAction())) {
final Bundle data = intent.getExtras();
if (data != null){
Data.Builder dataBuilder = new Data.Builder();
for ( String key : data.keySet()){
dataBuilder.putString(key,String.valueOf(data.get(key)));
Log.d(TAG, "onReceive: key:" +key+ " and keyValue "+data.get(key));
}
WorkManager mWorkManager = WorkManager.getInstance();
OneTimeWorkRequest mRequest = new OneTimeWorkRequest
.Builder(MessageSyncWorker.class)
.setInputData(dataBuilder.build())
.build();
mWorkManager.enqueue(mRequest);
Log.d(TAG, "onReceive: Data: " +data);
}
```
This is what am doing
on the WorkManager class
```
public class MessageSyncWorker extends Worker {
String sender, timestamp, msgID, text_message;
private static final String TAG = "MessageSyncWorker";
private static int counter = 0;
public MessageSyncWorker(@NonNull Context context,
@NonNull WorkerParameters workerParams) {
super(context, workerParams);
}
@NonNull
@Override
public Result doWork() {
Data inputData = getInputData();
Log.d(TAG, "doWork: " +" inputdata "+inputData +
" DataValueMAP "+inputData.getKeyValueMap());
Bundle extras = new Bundle();
for (String key : inputData.getKeyValueMap().keySet()) {
// extras.putString(key,String.valueOf(inputData.getString(key))); //This also works well
extras.putString(key,inputData.getString(key));
Log.d(TAG, "doWork: ExtrasKeySet " +extras.keySet());
Log.d(TAG, "doWork: Extra Extra: "+inputData.getString(key));
}
Log.d(TAG, "doWork: Extras: Tena "+extras);
processBundleData(extras);
return Result.success();
}
private void processBundleData (Bundle bundle){
if (bundle !=null){
Object[] newpdusObj = new Object[] { bundle.get("pdus") };
Log.d(TAG, "processBundleData: Bundle "+newpdusObj);
if (newpdusObj !=null){
for (int i = 0; i < newpdusObj.length; i++){
SmsMessage currentMessage =
SmsMessage.createFromPdu((byte[]) newpdusObj[i]);
Log.d(TAG, "processBundleData: CurrentSMS "+currentMessage);
}
}
}
}
```
Below is my logs showing the Exception,
```
2019-10-21 19:53:34.597 2364-2532/com.techweezy.smartsync D/MessageSyncWorker: processBundleData: Bundle [Ljava.lang.Object;@38e5886
2019-10-21 19:53:34.837 2364-2415/com.techweezy.smartsync E/WM-WorkerWrapper: Work [ id=66ed12df-eb08-4714-9360-932ad2280873, tags={ com.techweezy.smartsync.utils.MessageSyncWorker } ] failed because it threw an exception/error
java.util.concurrent.ExecutionException: java.lang.ClassCastException: java.lang.String cannot be cast to byte[]
at androidx.work.impl.utils.futures.AbstractFuture.getDoneValue(AbstractFuture.java:516)
at androidx.work.impl.utils.futures.AbstractFuture.get(AbstractFuture.java:475)
at androidx.work.impl.WorkerWrapper$2.run(WorkerWrapper.java:290)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to byte[]
at com.techweezy.smartsync.utils.MessageSyncWorker$override.processBundleData(MessageSyncWorker.java:74)
at com.techweezy.smartsync.utils.MessageSyncWorker$override.doWork(MessageSyncWorker.java:63)
at com.techweezy.smartsync.utils.MessageSyncWorker$override.access$dispatch(Unknown Source:119)
at com.techweezy.smartsync.utils.MessageSyncWorker.doWork(Unknown Source:12)