如何使用返回值或异常的静态方法创建单例类?

时间:2017-11-22 04:17:01

标签: android

我有一个应用程序,其中我试图获得正常工作的广告ID,但我想使用静态方法创建一个单例类,该方法返回广告ID或异常。我该怎么做

代码: -

 AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
        @Override
        protected String doInBackground(Void... params) {
            AdvertisingIdClient.Info id_Info = null;
            try {
                id_Info = AdvertisingIdClient.getAdvertisingIdInfo(getApplicationContext());
            } catch (IOException | GooglePlayServicesNotAvailableException | GooglePlayServicesRepairableException e) {
                e.printStackTrace();
            }
            String advertId = null;
            try {
                assert id_Info != null;
                advertId = id_Info.getId();
                Log.e(TAG, "Advertisement Id::::" + advertId);

            } catch (NullPointerException e) {
                e.printStackTrace();
            }
            return advertId;
        }
    };
    task.execute();

1 个答案:

答案 0 :(得分:0)

IdFetcher课程如下

  class IdFetcher {
          interface ResultCallback{
              void onId(String advertId);
              void onError(Exception e);
          }
          private IdFetcher idFetcher;
          private Context context;

          public IdFetcher get(Context context){
             if(idFetcher == null){
                idFetcher = new IdFetcher(); 
             }
            this.context = context;
            return idFetcher;
          }

          public void fetchId(ResultCallback rcb){
            AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
            @Override
            protected String doInBackground(Void... params) {
                AdvertisingIdClient.Info id_Info = null;
                try {
                    id_Info = AdvertisingIdClient.getAdvertisingIdInfo(getApplicationContext());
                } catch (IOException | GooglePlayServicesNotAvailableException | GooglePlayServicesRepairableException e) {
                  context.runOnUIThread(new Runnable(){
                      @Override
                      public void run(){
                        rcb.onError(e);
                      }
                    });
                }
                String advertId = null;
                try {
                    assert id_Info != null;
                    advertId = id_Info.getId();
                    Log.e(TAG, "Advertisement Id::::" + advertId);

                } catch (NullPointerException e) {
                   context.runOnUIThread(new Runnable(){
                      @Override
                      public void run(){
                        rcb.onError(e);
                      }
                    });
                }

                context.runOnUIThread(new Runnable(){
                      @Override
                      public void run(){
                        rcb.onId(advertId);
                      }
                    });
                return advertId;
               }
              };
              task.execute();
          }
    }

<强>用法

IdFetcher.get(context).fetchId(new IdFetcher.ResultCallback(){
    @Override
    public void onId(String advertId){
     // do something with id
    }

    @Override
    public void onError(Exception e){
     // do something with error
    }
});