2015-10-20 101 views
1

其他公用事業類我知道,服務可以從活動啓動如下安卓:從非活動

public class MainActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 

    // Method to start the service 
    public void startService(View view) { 
     startService(new Intent(getBaseContext(), MyService.class)); 
    } 
} 

爲startService()方法啓動的服務是有在Activity類,我想它不可能從任何不擴展活動類的Java類中調用服務... 如果有任何方法,我們可以從普通/ Utility類啓動服務,請讓我知道?

編輯:我曾嘗試下面的建議是,

package com.genedevelopers.shootthedevil; 

import android.app.Activity; 
import android.content.ComponentName; 
import android.content.Context; 
import android.content.Intent; 
import android.content.ServiceConnection; 
import android.graphics.Canvas; 
import android.graphics.Rect; 
import android.os.IBinder; 

public class Devil { 

    // This are starting data. 
    public static final float initSpeed = 5; 
    public static final long initTimeBetweenDucks = 1800; // in milliseconds 

    public static Context dctx; 
    private boolean mIsBound = false; 

    // This is current speed that will be increased and current time that will be decreased. 
    public static float speed; 
    public static long timeBetweenDucks; // in milliseconds 

    public static long timeOfLastDuck; 

    public static boolean direction = true; 

    // Needed for speeding up the game 
    public static long timeBetweenSpeedups = 250; // in milliseconds 
    public static long timeOfLastSpeedup; 


    // Devil position on the screen. 
    public float x; 
    public float y; 

    // Speed and direction. 
    private float velocity; 

    //MusicService musicS; 

    //For background Music start 
    private MusicService2 mServ; 
    private ServiceConnection Scon =new ServiceConnection(){ 

     public void onServiceConnected(ComponentName name, IBinder 
       binder) { 

      mServ = ((MusicService2.ServiceBinder)binder).getService(); 
     } 

     public void onServiceDisconnected(ComponentName name) { 
      mServ = null; 
     } 
    }; 

    void doBindService(){ 

     dctx.bindService(new Intent(dctx,MusicService2.class), 
       Scon, Context.BIND_AUTO_CREATE); 

     mIsBound = true; 
    } 

    void doUnbindService() 
    { 
     if(mIsBound) 
     { 
      dctx.unbindService(Scon); 
      mIsBound = false; 
     } 
    } 
    //For background Music end 

    public Devil(int y){ 
     this.y = y; 

     if(Devil.direction){ 
      this.x = Game.screenWidth; 
      velocity = speed * -1; 
     } else { 
      this.x = 0 - Game.duckImage.getWidth(); 
      velocity = speed; 
     } 

     doBindService(); 
     // We change direction for a next devil. 
     Devil.direction = !Devil.direction; 
     dctx=HighScore.ctx; 

    } 


    /** 
    * Move the devil. 
    */ 
    public void update(){ 
     this.x += velocity; 
    } 

    /** 
    * Draw the devil to a screen. 
    * 
    * @param canvas Canvas to draw on. 
    */ 
    public void draw(Canvas canvas){ 

    //  musicS=new MainMenu().getMusicServiceInstance(); 

     if(velocity < 0) 
      canvas.drawBitmap(Game.devilImage, x, y, null); 
     else 
      canvas.drawBitmap(Game.devilRightImage, x, y, null); 
    } 


    /** 
    * Checks if the devil was touched/shoot. 
    * 
    * @param touchX X coordinate of the touch. 
    * @param touchY Y coordinate of the touch. 
    * 
    * @return True if touch coordinates are in the coordinates of devil rectangle, false otherwise. 
    */ 
    public boolean wasItShoot(int touchX, int touchY){ 
     Rect devilRect = new Rect((int)this.x, (int)this.y, (int)this.x + Game.devilImage.getWidth(), (int)this.y + Game.devilImage.getHeight()); 



     if(duckRect.equals(true)){ 
      Intent music = new Intent(); 
      music.setClass(dctx,MusicService2.class); 
      dctx.startService(music); 
     } 
     return duckRect.contains(touchX, touchY); 
    } 

} 

,但它不能正常工作,請幫我...

回答

0

如果您通過上下文類(例如,在構造函數)

context.startService(intent)) 
0

在理論上可以,但你需要上下文來啓動服務。上下文通常是一個活動或服務(What is 'Context' on Android?)。您可以將上下文的參考傳遞給實用程序類並從那裏啓動服務。

0

startServiceContext而不是Activity的方法。只要你有一個上下文,你可以開始使用它的服務。

你可以做如下:

public class MyApp extends Application { 
    public static MyApp instance; 
    public void onCreate() { 
    super.onCreate() 
    instance = this; 
    } 
} 

然後從你能做MyApp.instance.startService(...)任何地方。

如果您這樣做,請確保您在清單中註冊您的應用程序類。

0

嗨thanku大家烏爾重播...現在它的工作...簡單的錯誤,如果(duckRect你可以啓動它。等於(true)){}從來都不是真的,所以它不縮放服務。