2013-06-19 15 views
0

我有這樣的代碼,我想從另一個類訪問此:如何在傳遞上下文時從另一個類調用方法?

public void nomorA(int num){ 
     try { 
      am = ctx.getAssets(); 
      AssetFileDescriptor afd = am.openFd("nom.wav"); 
      player = new MediaPlayer(); 
      player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); 
      player.prepare(); 
      player.start(); 
      player.setLooping(false); 
      Thread.sleep(1000); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

如何調用從Main Activity這種方法,並通過其上下文所調用方法?

+2

創建類的一個對象,並調用該方法' .nomor_urut(INT數)' –

回答

1

添加右鍵作爲第二個參數,使靜態函數

public class Utility{ 

    public static void nomorA(int num,Context ctx){ 

    } 
} 

呼叫功能:

Utility. nomorA(1,ClassName.this); 
+0

謝謝, 是工作 – aangwi

相關問題