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)){}从来都不是真的,所以它不缩放服务。