public class BackgroundService extends Service { @Override
public IBinder onBind(Intent arg0) {
return null;
}

@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "starting", Toast.LENGTH_SHORT).show();
System.out.println("service starting");
}

public int OnStartCommand(Intent intent, int flags, int startId){
super.onStartCommand(intent, flags, startId);
Toast.makeText(this, "service Start", Toast.LENGTH_SHORT).show();
System.out.println("service start");
return START_STICKY;

}

@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "service stop", Toast.LENGTH_SHORT).show();
System.out.println("service stop");
}
}这里面的OnStartCommand怎么执行不到

解决方案 »

  1.   

    有用Context.startService()启动么?
      

  2.   

    如果用startService()方法启动Service,OnStartCommand方法能够执行;
    如果用bindService()方法启动Service,OnstartCommand方法就不能够执行了。
      

  3.   


    startService(new Intent(MainActivity.this,BackgroundService.class));
    我是这样启动的!
      

  4.   

    没有什么问题呀,那你是否在manifest文件中对Service进行配置,你的startService(new Intent(MainActivity.this,BackgroundService.class));代码是否能够被调用到呢?
      

  5.   

    <service android:name="com.sample.BackgroundService" />
    OnCreate, OnDestory都可以被调用!
    和这个@Override有没有关系? 我在OnStartCommand前面手动加上@Overrid,要报错!
      

  6.   

    方法名写错了  @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
           
        }应该是这个样子,onStartCommand on 写成大写On了,