android BroadcastReceiver 广播 onReceive()执行多次
功能:下载listView中点击的item数据,
ListView的activity中代码:
@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(){}
//点几次listview,这里会执行几次
@Override
public void onReceive(Context arg0, Intent intent) {
Bundle bundle=intent.getExtras();
String resultString = bundle.getString("resultString");
System.out.println(resultString);
}
}
service的代码:
@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 += ",下载失败.";
} else if (result == 0) {
resultsString += ",下载成功.";
} else if (result == 1){
resultsString += ",文件已存在.";
}
Intent intent=new Intent();
intent.putExtra("resultString", resultsString);
intent.setAction(ConstantVariable.DOWNLOADACTION);
sendBroadcast(intent);
}
}
androidmanifest.xml注册service
<service android:name="com.nic.service.DownLoadService"></service>
其他的辅助类就不贴代码了。大家知道为什么会这样吗?
ListView的activity中代码:
@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(){}
//点几次listview,这里会执行几次
@Override
public void onReceive(Context arg0, Intent intent) {
Bundle bundle=intent.getExtras();
String resultString = bundle.getString("resultString");
System.out.println(resultString);
}
}
service的代码:
@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 += ",下载失败.";
} else if (result == 0) {
resultsString += ",下载成功.";
} else if (result == 1){
resultsString += ",文件已存在.";
}
Intent intent=new Intent();
intent.putExtra("resultString", resultsString);
intent.setAction(ConstantVariable.DOWNLOADACTION);
sendBroadcast(intent);
}
}
androidmanifest.xml注册service
<service android:name="com.nic.service.DownLoadService"></service>
其他的辅助类就不贴代码了。大家知道为什么会这样吗?
...全文
相关推荐
安卓短信彩信相关相关-BroadcastReceiver最基础之短信收发应用.rar
BroadcastReceiver最基础之短信收发应用.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
Android receiver 多次启动相同Activity
Android防止service多次执行startCommand
Android防止service多次执行startCommand 博客地址:http://blog.csdn.net/yuzhiqiang_1993/article/details/78211385
Android面试常问基础知识点.pdf
Android面试常问基础知识点
单元6Android的服务与广播应用程序设计.pptx
单元6Android的服务与广播应用程序设计.pptx



