2012-09-05 37 views
2

我正在構建一個包含ProgressBar的Widget。如果Widget正在計算,我將該ProgressBar的可見性設置爲VISIBLE,如果所有計算都停止,則設置爲INVISIBILE。應該沒有問題,因爲setVisibility記錄爲RemotableViewMethod。然而,HTC的一些人似乎忘記了它(即在Wildfire S上),因此致電RemoteViews.setVisibility將導致崩潰。出於這個原因,我嘗試執行檢查,如果setVisibility真的可以調用。我已經writen這個方法吧:確定一個方法是否是一個`RemotableViewMethod`

private boolean canShowProgress(){ 
     LogCat.d(TAG, "canShowProgress"); 
     Class<ProgressBar> barclz = ProgressBar.class; 
     try { 
      Method method = barclz.getMethod("setVisibility", new Class[]{int.class}); 
      Annotation[] anot = method.getDeclaredAnnotations(); 
      return anot.length > 0; 
     } catch (SecurityException e) { 
      LogCat.stackTrace(TAG, e); 
     } catch (NoSuchMethodException e) { 
      LogCat.stackTrace(TAG, e); 
     } 
     return false; 
    } 

這是可行的,但真的醜陋的,因爲它會返回`True'如果任何 Annotiation存在。我看了一下,自己是怎麼做遠程視窗的查找,發現這個:

if (!method.isAnnotationPresent(RemotableViewMethod.class)) { 
       throw new ActionException("view: " + klass.getName() 
         + " can't use method with RemoteViews: " 
         + this.methodName + "(" + param.getName() + ")"); 
      } 

但我便無法做同樣的,因爲Class RemotableViewMethod不通過SDK accsesible。如何知道它是否可訪問?

+0

嘿!你認爲這是你可能能夠回答的問題嗎? http://stackoverflow.com/questions/36679333/android-notification-remoteview-settranslationx-for-textview – Zen

回答

3

通過寫我的問題我有想通過它的名稱查找類,它的工作。 所以我我的方法更新爲以下幾點:

private boolean canShowProgress(){ 
     LogCat.d(TAG, "canShowProgress"); 
     Class<ProgressBar> barclz = ProgressBar.class; 
     try { 
      Method method = barclz.getMethod("setVisibility", new Class[]{int.class}); 
      Class c = null; 
      try { 
       c = Class.forName("android.view.RemotableViewMethod"); 
      } catch (ClassNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      return (this.showProgress= (c != null && method.isAnnotationPresent(c))); 


     } catch (SecurityException e) { 
      LogCat.stackTrace(TAG, e); 
     } catch (NoSuchMethodException e) { 
      LogCat.stackTrace(TAG, e); 

     } 
     return false; 
    } 

這完美的作品

相關問題