2012-05-04 105 views
0

我從來沒有做過太多的Java編程,主要是所有的PHP/RUBY/Javascript,所以我不完全確定如何可能去訪問wLock.release()onDestroy如何從類內的其他函數正確訪問方法?

public class SlammedNavigatorActivity extends DroidGap { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     super.loadUrl("file:///android_asset/www/index.html"); 

     PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 
     PowerManager.WakeLock wLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "SN Wake Lock"); 
     wLock.acquire(); 
    } 

    public void onDestroy() { 
     wLock.release(); 
    } 
} 

回答

4

你應該局部變量wLock轉換爲私有字段:

public class SlammedNavigatorActivity extends DroidGap { 
    private PowerManager.WakeLock wLock; 


    public void onCreate(Bundle savedInstanceState) { 
    // ... 
    wLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "SN Wake Lock"); 
    // ... 
    } 
} 
1

使它成爲一個實例變量:

public class SlammedNavigatorActivity extends DroidGap { 

    private PowerManager.WakeLock wLock; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     super.loadUrl("file:///android_asset/www/index.html"); 

     PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 
     this.wLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "SN Wake Lock"); 
     wLock.acquire(); 

    } 

    public void onDestroy() { 
     this.wLock.release(); 
    } 
} 

你爲什麼不學習語言基礎知識做之前Android編程?閱讀Java tutorial,或者一本關於Java的好書。

0

製作wLock類成員

public class SlammedNavigatorActivity extends DroidGap { 

    private PowerManager.WakeLock wLock; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     super.loadUrl("file:///android_asset/www/index.html"); 

     PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 
     wLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "SN Wake Lock"); 
     wLock.acquire(); 

    } 

    public void onDestroy() { 

     wLock.release(); 
    } 
}