Volley中Singleton类有什么用 [英] What is the use of Singleton class in Volley

查看:22
本文介绍了Volley中Singleton类有什么用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Singleton 类的帮助下使用 Volley 库下载图像.
问题:

I used Volley library to download a image with the help of Singleton class.
Problem :

如果我使用单吨类,我可以在一段时间内成功下载图像,而且我注意到图像也可以在不使用单吨类的情况下成功下载.

If I used a single ton class I can download a image successfully with in a time and also I noticed that image is downloaded successfully with out using the single ton class also.

请任何人告诉我在我的代码中使用单例类有什么好处.

Will you please any one tell me what are the benefits with the singleton class into my code .

-------------代码 使用单例类 --------------------

---------------------Code With Singleton Class --------------------

MainActivity.java

public class MainActivity extends AppCompatActivity {
    Button response_click;
    TextView text_response;
    RequestQueue requestQueue;
    String server_url="http://i.imgur.com/7spzG.png";
    ImageView imageView;
    ImageRequest imageRequest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        response_click=(Button) findViewById(R.id.click_response);
        text_response=(TextView) findViewById(R.id.text_response);
        imageView=(ImageView) findViewById(R.id.image_download);
    }//onCreate Ending
    public void response_click(View view){
   
        final ImageRequest imageRequest=new ImageRequest(server_url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                imageView.setImageBitmap(response);
            }
        }, 0, 0, null, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(),"You Got an Error....",Toast.LENGTH_LONG).show();

            }
        });
MySingleTon.getInstance(MainActivity.this).addToRequestQue(imageRequest);

    }//Button Click Ending
}//Main Activity Ending

MySingleTon.java

public class MySingleTon {
    private static MySingleTon mySingleTon;
    private RequestQueue requestQueue;
    private static Context mctx;
    private MySingleTon(Context context){
        this.mctx=context;
        this.requestQueue=getRequestQueue();

    }
    public RequestQueue getRequestQueue(){
        if (requestQueue==null){
            requestQueue= Volley.newRequestQueue(mctx.getApplicationContext());
        }
        return requestQueue;
    }
   public static synchronized MySingleTon getInstance(Context context){
       if (mySingleTon==null){
           mySingleTon=new MySingleTon(context);
       }
       return mySingleTon;
   }
    public<T> void addToRequestQue(Request<T> request){
        requestQueue.add(request);

    }
}

在这里,我编写了一个代码,用于在不使用 Singleton 类的情况下下载图像.请同时检查此代码.
在这里请记住一件事,在不使用 Singleton 类的情况下,我也完成了我的工作,没有任何错误.

Here, I write a code for downloading the image with out using the Singleton class. Please Check this code also.
Here remember one thing, With out using the Singleton class also I done my job with out any errors.

------------------------代码 没有单例类 -----------------------

------------------------code With Out Singleton class -----------------------

MainActivity.java

public class MainActivity extends AppCompatActivity {
    Button response_click;
    TextView text_response;
    RequestQueue requestQueue;
    String server_url="http://i.imgur.com/7spzG.png";
    ImageView imageView;
    ImageRequest imageRequest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        response_click=(Button) findViewById(R.id.click_response);
        text_response=(TextView) findViewById(R.id.text_response);
        imageView=(ImageView) findViewById(R.id.image_download);
    }//onCreate Ending
    public void response_click(View view){
        requestQueue=Volley.newRequestQueue(this.getApplicationContext());
        final ImageRequest imageRequest=new ImageRequest(server_url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                imageView.setImageBitmap(response);
            }
        }, 0, 0, null, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(),"You Got an Error....",Toast.LENGTH_LONG).show();

            }
        });

        requestQueue.add(imageRequest);
        
    }//Button Click Ending
}//Main Activity Ending

推荐答案

如果您的应用程序不断使用网络,那么设置一个 RequestQueue 实例可能是最有效的,该实例将持续您的应用程序的生命周期.您可以通过多种方式实现这一目标.推荐的方法是实现一个封装 RequestQueue 和其他 Volley 功能的单例类.另一种方法是子类化 Application 并在 Application.onCreate() 中设置 RequestQueue.但不鼓励这种方法;静态单例可以以更加模块化的方式提供相同的功能.

If your application makes constant use of the network, it's probably most efficient to set up a single instance of RequestQueue that will last the lifetime of your app. You can achieve this in various ways. The recommended approach is to implement a singleton class that encapsulates RequestQueue and other Volley functionality. Another approach is to subclass Application and set up the RequestQueue in Application.onCreate(). But this approach is discouraged; a static singleton can provide the same functionality in a more modular way.

一个关键概念是 RequestQueue 必须使用应用程序上下文而不是活动上下文进行实例化.这可确保 RequestQueue 将在您的应用的整个生命周期内持续存在,而不是在每次重新创建 Activity 时(例如,当用户旋转设备时)重新创建.

A key concept is that the RequestQueue must be instantiated with the Application context, not an Activity context. This ensures that the RequestQueue will last for the lifetime of your app, instead of being recreated every time the activity is recreated (for example, when the user rotates the device).

阅读文档 https://developer.android.com/training/volley/requestqueue.html#singleton

这篇关于Volley中Singleton类有什么用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆