我試圖在onHandleIntent()我IntentService的方法註冊DownloadManager.ACTION_DOWNLOAD_COMPLETE接收機和在的onDestroy註銷所述接收器()的IntentService的方法。但是我認爲它沒有被註冊,因爲一旦下載完成,接收器的onReceive()方法就不會被觸發。 任何人都可以幫助我嗎?廣播接收機在IntentService
2
A
回答
0
下面的代碼是from here:
public class MyWebRequestService extends IntentService{
public static final String REQUEST_STRING = "myRequest";
public static final String RESPONSE_STRING = "myResponse";
public static final String RESPONSE_MESSAGE = "myResponseMessage";
private String URL = null;
private static final int REGISTRATION_TIMEOUT = 3 * 1000;
private static final int WAIT_TIMEOUT = 30 * 1000;
public MyWebRequestService() {
super("MyWebRequestService");
}
@Override
protected void onHandleIntent(Intent intent) {
String requestString = intent.getStringExtra(REQUEST_STRING);
String responseString = requestString + " " + DateFormat.format("MM/dd/yy h:mmaa", System.currentTimeMillis());
String responseMessage = "";
SystemClock.sleep(10000); // Wait 10 seconds
Log.v("MyWebRequestService:",responseString);
// Do some really cool here
// I am making web request here as an example...
try {
URL = requestString;
HttpClient httpclient = new DefaultHttpClient();
HttpParams params = httpclient.getParams();
HttpConnectionParams.setConnectionTimeout(params, REGISTRATION_TIMEOUT);
HttpConnectionParams.setSoTimeout(params, WAIT_TIMEOUT);
ConnManagerParams.setTimeout(params, WAIT_TIMEOUT);
HttpGet httpGet = new HttpGet(URL);
HttpResponse response = httpclient.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseMessage = out.toString();
}
else{
//Closes the connection.
Log.w("HTTP1:",statusLine.getReasonPhrase());
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
Log.w("HTTP2:",e);
responseMessage = e.getMessage();
} catch (IOException e) {
Log.w("HTTP3:",e);
responseMessage = e.getMessage();
}catch (Exception e) {
Log.w("HTTP4:",e);
responseMessage = e.getMessage();
}
Intent broadcastIntent = new Intent();
broadcastIntent.setAction(MyWebRequestReceiver.PROCESS_RESPONSE);
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
broadcastIntent.putExtra(RESPONSE_STRING, responseString);
broadcastIntent.putExtra(RESPONSE_MESSAGE, responseMessage);
sendBroadcast(broadcastIntent);
}
}
IntentFilter filter = new IntentFilter(MyWebRequestReceiver.PROCESS_RESPONSE);
filter.addCategory(Intent.CATEGORY_DEFAULT);
receiver = new MyWebRequestReceiver();
registerReceiver(receiver, filter);
@Override
public void onDestroy() {
this.unregisterReceiver(receiver);
super.onDestroy();
}
public class MyWebRequestReceiver extends BroadcastReceiver{
public static final String PROCESS_RESPONSE = "com.as400samplecode.intent.action.PROCESS_RESPONSE";
@Override
public void onReceive(Context context, Intent intent) {
String responseString = intent.getStringExtra(MyWebRequestService.RESPONSE_STRING);
String reponseMessage = intent.getStringExtra(MyWebRequestService.RESPONSE_MESSAGE);
TextView myTextView = (TextView) findViewById(R.id.response);
myTextView.setText(responseString);
WebView myWebView = (WebView) findViewById(R.id.myWebView);
myWebView.getSettings().setJavaScriptEnabled(true);
try {
myWebView.loadData(URLEncoder.encode(reponseMessage,"utf-8").replaceAll("\\+"," "), "text/html", "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
public class MyWebRequestService extends IntentService{
public static final String REQUEST_STRING = "myRequest";
public static final String RESPONSE_STRING = "myResponse";
public static final String RESPONSE_MESSAGE = "myResponseMessage";
private String URL = null;
private static final int REGISTRATION_TIMEOUT = 3 * 1000;
private static final int WAIT_TIMEOUT = 30 * 1000;
public MyWebRequestService() {
super("MyWebRequestService");
}
@Override
protected void onHandleIntent(Intent intent) {
String requestString = intent.getStringExtra(REQUEST_STRING);
String responseString = requestString + " " + DateFormat.format("MM/dd/yy h:mmaa", System.currentTimeMillis());
String responseMessage = "";
SystemClock.sleep(10000); // Wait 10 seconds
Log.v("MyWebRequestService:",responseString);
// Do some really cool here
// I am making web request here as an example...
try {
URL = requestString;
HttpClient httpclient = new DefaultHttpClient();
HttpParams params = httpclient.getParams();
HttpConnectionParams.setConnectionTimeout(params, REGISTRATION_TIMEOUT);
HttpConnectionParams.setSoTimeout(params, WAIT_TIMEOUT);
ConnManagerParams.setTimeout(params, WAIT_TIMEOUT);
HttpGet httpGet = new HttpGet(URL);
HttpResponse response = httpclient.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseMessage = out.toString();
}
else{
//Closes the connection.
Log.w("HTTP1:",statusLine.getReasonPhrase());
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
Log.w("HTTP2:",e);
responseMessage = e.getMessage();
} catch (IOException e) {
Log.w("HTTP3:",e);
responseMessage = e.getMessage();
}catch (Exception e) {
Log.w("HTTP4:",e);
responseMessage = e.getMessage();
}
Intent broadcastIntent = new Intent();
broadcastIntent.setAction(MyWebRequestReceiver.PROCESS_RESPONSE);
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
broadcastIntent.putExtra(RESPONSE_STRING, responseString);
broadcastIntent.putExtra(RESPONSE_MESSAGE, responseMessage);
sendBroadcast(broadcastIntent);
}
}
IntentFilter filter = new IntentFilter(MyWebRequestReceiver.PROCESS_RESPONSE);
filter.addCategory(Intent.CATEGORY_DEFAULT);
receiver = new MyWebRequestReceiver();
registerReceiver(receiver, filter);
@Override
public void onDestroy() {
this.unregisterReceiver(receiver);
super.onDestroy();
}
public class MyWebRequestReceiver extends BroadcastReceiver{
public static final String PROCESS_RESPONSE = "com.as400samplecode.intent.action.PROCESS_RESPONSE";
@Override
public void onReceive(Context context, Intent intent) {
String responseString = intent.getStringExtra(MyWebRequestService.RESPONSE_STRING);
String reponseMessage = intent.getStringExtra(MyWebRequestService.RESPONSE_MESSAGE);
TextView myTextView = (TextView) findViewById(R.id.response);
myTextView.setText(responseString);
WebView myWebView = (WebView) findViewById(R.id.myWebView);
myWebView.getSettings().setJavaScriptEnabled(true);
try {
myWebView.loadData(URLEncoder.encode(reponseMessage,"utf-8").replaceAll("\\+"," "), "text/html", "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
在清單創建服務。
+0
撰寫關於鏈接內容的摘要。今後,鏈接可能已經死亡 –
+0
確定現在編輯,感謝您的建議。 –
+0
如果您在答案中複製/粘貼代碼,請始終添加屬性。此外,代碼只有答案是不鼓勵,添加說明這個代碼如何解決問題中的問題! –
2
創建服務類,
public class ConnectionBroadReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager cm = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
IConnectionCallback callback = (IConnectionCallback) context;
callback.finishDownload();
}
在活動,
ConnectionBroadReceiver broadReceiver = new ConnectionBroadReceiver();
registerReceiver(broadReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
創建界面,並在活動實施並定義功能你要下載
public interface IConnectionCallback {
void finishDownload();
}
後做
並最終在清單中註冊該服務,
<receiver android:name=".ConnectionBroadReceiver">
相關問題
- 1. 廣播接收器,IntentService和GPS回調
- 2. 廣播接收機問題
- 3. 廣播接收機android
- 4. ConnectivityManager.CONNECTIVITY_ACTION廣播接收機
- 5. 在Windows 10手機中接收廣播
- 6. 廣播接收
- 7. IntentService中的廣播接收器(服務已泄漏意向接收器)
- 8. 廣播接收器
- 9. 廣播接收器
- 10. 我的廣播接收機不能得到BOOT_COMPLETED廣播
- 11. Android的剪貼板廣播接收機
- 12. 的android廣播接收機通知
- 13. 廣播接收機的生命週期
- 14. 如何使用android廣播接收機
- 15. 廣播接收機空指針的Android
- 16. 廣播接收器從相機
- 17. 引導完成廣播接收機
- 18. 訪問使用廣播接收機
- 19. 關於廣播接收機android
- 20. 問題的理解廣播接收機
- 21. 沒有得到廣播接收機類
- 22. 重新啓動廣播接收機?
- 23. 廣播接收器的的onReceive()未能接收由另一廣播接收器
- 24. 接收呼叫的廣播接收機的優先級
- 25. 廣播接收器不能接收
- 26. 在android中處理未接電話與廣播接收機
- 27. 廣播接收器沒有收到廣播
- 28. GCM廣播接收器
- 29. C2DM廣播接收器
- 30. 廣播接收器僅
'ontent_Service'在onHandleIntent()結束後即被銷燬。一個寫得很好的'IntentService'只運行一會兒,所以你只會在短時間內收到這個廣播。 – CommonsWare
噢好吧。那麼,這是什麼替代解決方案,因爲我需要在後臺下載文件? –
因爲我不知道你在用什麼'IntentService',所以我不能告訴你答案是「不要使用'IntentService'」或者「在其他地方註冊你的接收器,比如在清單中」。 – CommonsWare