[英]Thread Inside Broadcast Receiver Android
獲得提示后,我得到了DownloadManager.ACTION_DOWNLOAD_COMPLETE廣播。我需要提取zip文件並填充數據庫,我已經編寫了代碼來完成工作。但是我必須在此處啟動Thread來處理文件的提取和插入。數據庫。
我知道不能在廣播接收器內部啟動線程。 如果有人遇到問題,請提出解決方案..因為我的工作需要花費一些時間才能插入,所以我不想等待並堅持下去。
在廣播接收器中運行線程時出現異常,並且
編寫此代碼在廣播接收器中也不起作用
BroadcastReceiver onComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
/*
* check status message whether the last queue is successful and
* completed and then stop self
*/
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) {
Intent mIntent = new Intent(downloadmanager1.this, DBService.class);
mIntent.putExtra("messenger", new Messenger(mHandler));
mIntent.putExtra("enqueue", enqueue);
ctxt.startService(mIntent);
// ctxt.startService(mIntent);
}
}
注意-調試時不會發生異常或錯誤,也不會在DDMS中啟動任何Intentservice
};
步驟#1:編寫一個IntentService
來完成“處理文件提取和插入數據庫”的工作。
步驟#2:從BroadcastReceiver
onReceive()
調用startService()
以將控制權傳遞給IntentService
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.