我创建了一个DownloadManager来从RecyclerView适配器中下载两个文件,并从项目中获取链接,
下载后,将文件保存在外部存储中,将其复制到内部,然后从外部删除,
然后我将路径保存到SQLite db上的文件,然后在另一个RecyclerView上显示它。
到目前为止,一切正常
唯一的事情是,它将行多次保存在db上,就像代码在循环中一样,我这样说是因为我为db ops和下载完成时设置了一个祝酒词,并且这些祝酒词显示为多个时间,以及保存在数据库中的条目。
在另一个回收站视图中,出现了多个相同的项目
我设置了一个onclick侦听器,并在Click上执行了此代码
holder.downloadBTN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if (CheckIsDataAlreadyInDBorNot(ctx, DBConstants.SavedItemsEntry.TABLE_NAME, DBConstants.SavedItemsEntry.COLUMN_TITLE, Title)) {
Toast.makeText(ctx, "Item ja Existente", Toast.LENGTH_SHORT).show();
}else {
DownloadClick(pdfurl, image, Title, Title);
BroadCReciever(Title, desc);
}
}catch (Exception e){
}
}
});
似乎循环的代码在BroadCast接收器上
这是广播接收者的代码:
private void BroadCReciever(final String filename, final String desc){
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)){
//IMAGE REQUEST QUERY SOLVER
DownloadManager.Query req_query = new DownloadManager.Query();
req_query.setFilterById(queue_id_img);
final Cursor c = dm.query(req_query);
if (c.moveToFirst()) checkStatus(c,"image");
{
int columnindex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL==c.getInt(columnindex))
{
//DATA OBJECT
String UriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
final Uri i = Uri.parse(UriString);
final File f = new File(i.getPath());
try {
Bitmap im;
im = MediaStore.Images.Media.getBitmap(ctx.getContentResolver(),i);
// Initializing a new file
// The bellow line return a directory in internal storage
File fileimg = ctx.getDir("Images",MODE_PRIVATE);
// Create a file to save the image
fileimg = new File(fileimg, filename+".jpg");
try {
OutputStream stream = null;
stream = new FileOutputStream(fileimg);
im.compress(Bitmap.CompressFormat.JPEG,100,stream);
stream.flush();
stream.close();
}catch (IOException e) // Catch the exception
{
e.printStackTrace();
}
//checking is image file exists in Internal Storage
if(fileimg.exists()) {
String internal_uri = fileimg.getAbsolutePath();
img_internal_uri = internal_uri;
} else {
Toast.makeText(ctx, "doesnt exist image", Toast.LENGTH_SHORT).show();
}
//deleting file from external storage
if (f.exists()){
Boolean deleted = f.delete();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//IMAGE REQUEST QUERY SOLVER END
//PDF REQUEST QUERY SOLVER
DownloadManager.Query req_query2 = new DownloadManager.Query();
req_query2.setFilterById(queue_id_pdf);
final Cursor c2 = dm.query(req_query2);
if (c2.moveToFirst()) checkStatus(c2,"pdf");
{
int columnindex2 = c2.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL==c2.getInt(columnindex2))
{
//DATA OBJECT
String UriString = c2.getString(c2.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
final Uri i = Uri.parse(UriString);
final File f = new File(i.getPath());
if (f.exists()){
// Initializing a new file
// The bellow line return a directory in internal storage
File file = ctx.getDir("PDF",MODE_PRIVATE);
// Create a file to save the pdf
file = new File(file,filename+".pdf");
try {
OutputStream outstream = new FileOutputStream(file);
InputStream instream = new FileInputStream(f);
byte[] buffer = new byte[1024];
int length;
while ((length = instream.read(buffer)) > 0) {
outstream.write(buffer, 0, length);
}
instream.close();
outstream.flush();
outstream.close();
}catch (IOException e) // Catch the exception
{
e.printStackTrace();
}
if (file.exists()) {
String internal_uri = file.getAbsolutePath();
pdf_internal_uri = internal_uri;
} else {
Toast.makeText(ctx, "doesnt exist pdf", Toast.LENGTH_SHORT).show();
}
Boolean deleted = f.delete();
}
}
}
//PDF REQUEST QUERY SOLVER END
checkifDOne(c,c2,filename,desc,img_internal_uri,pdf_internal_uri);
}
}
};
ctx.registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
checkifDOne方法是一种保存数据的方法 在这里:
public void checkifDOne(Cursor c, Cursor c2, String Title, String desc, String img, String pdf){
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
int status = c.getInt(columnIndex);
int columnIndex2 = c2.getColumnIndex(DownloadManager.COLUMN_STATUS);
int status2 = c2.getInt(columnIndex);
if (status == DownloadManager.STATUS_SUCCESSFUL && status2 == DownloadManager.STATUS_SUCCESSFUL){
saveToDB(ctx,Title,desc,img,pdf);
}
}
,在该方法中,我们有:
public void saveToDB(Context c,String title, String desc, String img, String pdf){
DBAdapter db = new DBAdapter(c);
db.openDB();
long result = db.add(title,desc,img,pdf);
if (result == 1){
Toast.makeText(c, "item salvo para leitura offline", Toast.LENGTH_LONG).show();
Toast.makeText(c, img, Toast.LENGTH_SHORT).show();
Toast.makeText(c, pdf, Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(c, "Erro", Toast.LENGTH_SHORT).show();
}
db.CloseDB();
}
答案 0 :(得分:0)
问题是您错过了使用BroadcastReceiver。目前,每次调用 BroadCReceiver 函数时,您都会创建并注册一个新的接收器。因此,如果您打电话十次,那么您将完成十次工作。
您需要做的是摆脱 BroadCReceiver 函数,仅将receiver
声明保留为类中的字段,只需保留以下内容即可:
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)){
//IMAGE REQUEST QUERY SOLVER
DownloadManager.Query req_query = new DownloadManager.Query();
req_query.setFilterById(queue_id_img);
final Cursor c = dm.query(req_query);
if (c.moveToFirst()) checkStatus(c,"image");
{
int columnindex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL==c.getInt(columnindex))
{
//DATA OBJECT
String UriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
final Uri i = Uri.parse(UriString);
final File f = new File(i.getPath());
try {
Bitmap im;
im = MediaStore.Images.Media.getBitmap(ctx.getContentResolver(),i);
// Initializing a new file
// The bellow line return a directory in internal storage
File fileimg = ctx.getDir("Images",MODE_PRIVATE);
// Create a file to save the image
fileimg = new File(fileimg, filename+".jpg");
try {
OutputStream stream = null;
stream = new FileOutputStream(fileimg);
im.compress(Bitmap.CompressFormat.JPEG,100,stream);
stream.flush();
stream.close();
}catch (IOException e) // Catch the exception
{
e.printStackTrace();
}
//checking is image file exists in Internal Storage
if(fileimg.exists()) {
String internal_uri = fileimg.getAbsolutePath();
img_internal_uri = internal_uri;
} else {
Toast.makeText(ctx, "doesnt exist image", Toast.LENGTH_SHORT).show();
}
//deleting file from external storage
if (f.exists()){
Boolean deleted = f.delete();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//IMAGE REQUEST QUERY SOLVER END
//PDF REQUEST QUERY SOLVER
DownloadManager.Query req_query2 = new DownloadManager.Query();
req_query2.setFilterById(queue_id_pdf);
final Cursor c2 = dm.query(req_query2);
if (c2.moveToFirst()) checkStatus(c2,"pdf");
{
int columnindex2 = c2.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL==c2.getInt(columnindex2))
{
//DATA OBJECT
String UriString = c2.getString(c2.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
final Uri i = Uri.parse(UriString);
final File f = new File(i.getPath());
if (f.exists()){
// Initializing a new file
// The bellow line return a directory in internal storage
File file = ctx.getDir("PDF",MODE_PRIVATE);
// Create a file to save the pdf
file = new File(file,filename+".pdf");
try {
OutputStream outstream = new FileOutputStream(file);
InputStream instream = new FileInputStream(f);
byte[] buffer = new byte[1024];
int length;
while ((length = instream.read(buffer)) > 0) {
outstream.write(buffer, 0, length);
}
instream.close();
outstream.flush();
outstream.close();
}catch (IOException e) // Catch the exception
{
e.printStackTrace();
}
if (file.exists()) {
String internal_uri = file.getAbsolutePath();
pdf_internal_uri = internal_uri;
} else {
Toast.makeText(ctx, "doesnt exist pdf", Toast.LENGTH_SHORT).show();
}
Boolean deleted = f.delete();
}
}
}
//PDF REQUEST QUERY SOLVER END
checkifDOne(c,c2,filename,desc,img_internal_uri,pdf_internal_uri);
}
}
};
因此,receiver
现在在您的班级中是独一无二的。
然后您需要在 Activity?(或您正在使用的其他主类)生命周期中进行注册,并根据需要取消注册。例如,您可以在onStart中注册它,而在onStop中取消注册,例如:
@Override
public void onStart() {
super.onStart();
registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
@Override
public void onStop() {
super.onStop();
unregisterReceiver(receiver);
}
最后,当您要发送广播事件(在downloadBTN上单击事件)时,只需执行以下操作:
Intent intent = new Intent();
intent.setAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
context.sendBroadcast(intent);
这样,您只有一个已注册/未注册的接收器,并且每次单击只能正确处理一次任务。
或者,您可以在清单中注册接收者。如果您需要我,我可以解释如何做