【发布时间】:2016-04-24 15:39:07
【问题描述】:
我在In_cs_3_s1 类中编写了一个方法pdfviewer(...),它基本上将文件路径、文件名和URL 作为参数。它首先检查文件是否存在于指定路径中,如果存在则调用 pdf 查看器。否则它会从指定的 URL 下载文件:为此它调用 DOWNLOAD MANAGER。我想从很多地方调用这个method(pdfviewer)。
这是我的代码:
public void pdfviewer(String branch,String sem,String folder,String fname,String url,Context a)
{
File file = new File(Environment.getExternalStorageDirectory()
+File.separator+"name"+File.separator+branch+File.separator+sem+File.separator+folder,fname);
if(file.exists())
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),"application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try
{
startActivity(intent);
}
catch (ActivityNotFoundException e)
{
Toast.makeText( this, "NO pdf viewer found", Toast.LENGTH_SHORT).show();
}
}
else
{
file_download(url,branch,sem,folder,fname);
Toast.makeText( this, "Downloading.....", Toast.LENGTH_LONG).show();
}
}
public void file_download(String uRl,String branch,String sem,String folder,String fname) {
File direct = new File(Environment.getExternalStorageDirectory()
+File.separator+"name"+File.separator+branch+File.separator
+sem+File.separator+folder);
if (!direct.exists()) {
direct.mkdirs();
}
DownloadManager mgr = (DownloadManager) this.getSystemService(Context.DOWNLOAD_SERVICE);
Uri downloadUri = Uri.parse(uRl);
DownloadManager.Request request = new DownloadManager.Request(
downloadUri);
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI
| DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setDestinationInExternalPublicDir("/name"+File.separator+branch+File.separator
+sem+File.separator+folder,fname);
mgr.enqueue(request);
}
我想从按钮的 onclicklistener 调用此方法。我是新手,所以请放轻松。
【问题讨论】:
-
您不能创建一个 Activity 的新实例并直接使用它。新实例只能由 Android 系统创建。请在开始编程之前阅读有关android的基础知识,否则您将遇到数百个这样的问题。
-
谢谢!因此,如果我将方法放在不扩展 Activity 的不同类中,那么它应该可以正常工作吗?
-
好的,我明白了!我是新手,犯错并没有错。有没有办法让所有其他类都可以使用该方法。
标签: android onclicklistener buttonclick