Saturday, January 25, 2014

android BroadcastReceiver broadcast onReceive () is executed multiple times


            
Function: Download listView click the item data,

The activity ListView code:

@ Override
protected void onListItemClick (ListView l, View v, int position, long id) {
Mp3Info mp3Info = this.mp3Infos.get (position);
Intent intent = new Intent ();
intent.putExtra ("mp3Info", mp3Info);
intent.setClass (this, DownLoadService.class);
this.startService (intent);
receiver = new MyReceiver ();
IntentFilter filter = new IntentFilter ();
filter.addAction (ConstantVariable.DOWNLOADACTION);
Mp3ListActivity.this.registerReceiver (receiver, filter);
super.onListItemClick (l, v, position, id);
}


class MyReceiver extends BroadcastReceiver {


public MyReceiver () {}

/ / Point several times listview, here is performed several times

@ Override
public void onReceive (Context arg0, Intent intent) {
Bundle bundle = intent.getExtras ();
String resultString = bundle.getString ("resultString");
System.out.println (resultString);
}

}
service code:

@ Override
public int onStartCommand (Intent intent, int flags, int startId) {
Mp3Info mp3Info = (Mp3Info) intent.getSerializableExtra ("mp3Info");
DownLoadThread downLoadThread = new DownLoadThread (mp3Info);
Thread thread = new Thread (downLoadThread);
thread.start ();
return super.onStartCommand (intent, flags, startId);
}


class DownLoadThread implements Runnable {


Mp3Info mp3Info = null;


public DownLoadThread (Mp3Info mp3Info) {
this.mp3Info = mp3Info;
}


@ Override
public void run () {
String mp3Url = "";
try {
mp3Url = ConstantVariable.SERVER_URL
+ URLEncoder.encode (mp3Info.getMp3Name (), "UTF-8");
} Catch (UnsupportedEncodingException e) {
e.printStackTrace ();
}
HttpDownloader httpDownloader = new HttpDownloader ();
int result = httpDownloader.downFile (mp3Url, "mp3 /",
mp3Info.getMp3Name ());
String resultsString = mp3Info.getMp3Name ();
if (result == -1) {
resultsString + = ", download failed.";
} Else if (result == 0) {
resultsString + = ", the download was successful.";
} Else if (result == 1) {
resultsString + = ", the file already exists.";
}
Intent intent = new Intent ();
intent.putExtra ("resultString", resultsString);
intent.setAction (ConstantVariable.DOWNLOADACTION);
sendBroadcast (intent);
}
}
androidmanifest.xml registered service


Other auxiliary class is not posted code. We all know why this?
Reply:
debug mode facie, generally performed three times, I do not know why, it is estimated that when loaded asynchronously, and broadcast confusion, the specific times you execute this code, you use debug mode to see to know, too deep too difficult to digest,,,
Reply:
See the code,
If you do not perform many times before the problem.
Reply:
Every click on a list item must re-establish a broadcasting receiving it? How did not see unregistered
Reply:
reference to the third floor li_danwang reply:
click on a list item every time you re-establish a radio receiver? How did not see unregistered

Words awoke dreamer. Thank you very much.

No comments:

Post a Comment