2012-10-23 45 views
0

如果我有一個計時器任務內嵌在一個服務類中,並且該服務類有一個公共方法..我怎麼能從我的服務類中調用公共方法TimerTask的?如何在類中的服務中調用公共方法

class uiCheckTask extends TimerTask { 
     Boolean secDialog = false; 
     NavOverrideService myService; 

     public void run() { 
      try { 
       ActivityManager am = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE); 
       List<ActivityManager.RunningAppProcessInfo> processList = am.getRunningAppProcesses(); 

       String cProcess = processList.get(0).processName; 

       if (!cProcess.equals("com.example.services_test") && !secDialog) { 
        // myService.launchSecurity(); /// HELP HERE! 
       } else { 

       } 
      } catch(Exception ex) { 

      } 
     } 
    } 

launchSecuity:

public void launchSecurity(){ 
    Log.v("LAUNCHING", "((((((((((((((((((((SECURITY)))))))))))))))))"); 
    Intent intent = new Intent(this, SecurityService.class); 
    startService(intent); 
} 
+0

實例化類並調用方法? –

+0

我試過了,但我得到一個零點異常..該服務已經運行,並開始timerTask開始.. – erik

+0

launchSecurity做什麼?它可以是靜態的嗎? – hBrent

回答

1

如果uiCheckTask是一個內部類的Service的,那麼你可以調用通過

(MyService.this).launchSecurity() 

公共方法請注意,MyService是類名你的服務,而不是一個實例或本地變量。

相關問題