देखो, मैं निम्नलिखित कोड है:कैसे हैंडलर पर निर्भर करता है ReceiveResult (परिणाम रिसीवर) लागू किया जाता है?
मेरे कार्रवाई:
final Intent intent = new Intent(getApplicationContext(), MyService.class)
.putExtra(UploadService.EXTRA_RESULT_RECEIVER, new ResultReceiver(null) {
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
super.onReceiveResult(resultCode, resultData);
String result = resultData.getString(MyService.EXTRA_RESULT_SUCCESS);
...
imageView.setBackgroundDrawable(bitmap);// here my code fails
}
})
MyService:
Bundle b = new Bundle();
b.putString(EXTRA_RESULT_SUCCESS, response.toString());
resultReceiver.send(0, b);
और अपने आवेदन निम्नलिखित के साथ लाइन पर विफल रहता है "imageView.setBackgroundDrawable (बिटमैप)" अपवाद:
11-13 16:25:38.986: ERROR/AndroidRuntime(3586): FATAL EXCEPTION: IntentService[MyService]
android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
लेकिन यह डो जब मैं इस तरह रिसीवर को परिभाषित करता हूं (
new ResultReceiver(new Handler()){.../*here goes the same code as in the first example. nothing has been changed*/}
तो। जब मैं डिफ़ॉल्ट हैंडलर पास करता हूं तो यह असफल नहीं होता है। और मैं क्यों पूछता हूं? दोनों तरीकों से मेरा कोड लागू किया गया है, लेकिन जब कोई हैंडलर निर्दिष्ट नहीं करता है तो यह विफल हो जाता है। हैंडलर का क्या प्रभाव है?
मुझे संदेह है। क्या वह परिणाम Reseceiver.onReceiveResult हमेशा गैर-यूआई थ्रेड पर चलाएगा? –
नहीं, यह है कि एक हैंडलर गारंटी का उपयोग करके यह सही धागे पर चलाएगा। आम तौर पर कोई गारंटी नहीं दी जाती है कि रिसीवर को किस धागे पर बुलाया जाएगा। – Femi
समझ गया! यह परिणाम रिसीवर को बुलाए धागे पर आधारित है। त्वरित उत्तर के लिए धन्यवाद। –