【问题标题】:Is there a way to make the following method available to all the classes?有没有办法让所有类都可以使用以下方法?
【发布时间】: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


【解决方案1】:

为对象创建一个全局变量:(在onCreate方法之外)在第一个代码中

In_cs_3_s1 pobj;

问题在onClick内部,找不到pobj对象;

【讨论】:

  • 好吧,首先实例化一个 Activity 是完全错误的。 (2) 我认为代码独立于活动生命周期。所以创建另一个类并将这些方法放在该类中,然后在 onClick 方法中调用该函数。如果您需要活动的上下文,请将活动上下文作为函数参数传递。希望这会有所帮助
猜你喜欢
  • 2011-09-12
  • 2020-06-07
  • 2020-08-02
  • 2015-09-20
  • 2015-11-11
  • 2015-09-12
  • 2011-09-11
  • 1970-01-01
相关资源
最近更新 更多