jugel 2016-04-21 05:55 采纳率: 50%
浏览 2348

startService没反应,也没有报错,Logcat没任何信息,请大哥大姐们帮看看,呆马如下:

public class MainActivity extends Activity implements OnClickListener {
private Button start;
private Button stop;
private Button bind;
private Button unbind;
private MyService.DownloadBinder downloadBinder;
private ServiceConnection connection = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {

    }
    public void onServiceConnected(ComponentName name, IBinder service) {
        downloadBinder = (MyService.DownloadBinder) service;
        downloadBinder.startDownload();
        downloadBinder.getProgress();
    }
};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    start = (Button) findViewById(R.id.start_service);
    stop = (Button) findViewById(R.id.stop_service);
    bind = (Button) findViewById(R.id.bind);
    unbind = (Button) findViewById(R.id.unbind);
    start.setOnClickListener(this);
    stop.setOnClickListener(this);
    bind.setOnClickListener(this);
    unbind.setOnClickListener(this);
}

public void onClick(View v) {
    switch(v.getId()) {
    case R.id.start_service:
        Intent startIntent = new Intent(this, MyService.class);
        startService(startIntent);
        break;
    case R.id.stop_service:
        Intent stopIntent = new Intent(this, MyService.class);
        stopService(stopIntent);
        break;
    case R.id.bind:
        Intent bindIntent = new Intent(this, MyService.class);
        bindService(bindIntent, connection, BIND_AUTO_CREATE);
        break;
    case R.id.unbind:

        unbindService(connection);
        break;
    default:
        break;
    }
}

}

public class MyService extends Service {
private DownloadBinder db = new DownloadBinder();
class DownloadBinder extends Binder {
public void startDownload() {
Log.v("MyService", "download");
}
public int getProgress() {
return 0;
}
}

@Override
public IBinder onBind(Intent intent) {

    return db;
}
public void onCreate() {
    super.onCreate();
    Log.d("MyService", "start");
}
public int  onStartCommand(Intent intent , int flags, int startId) {
    Log.d("MyService", "startedCommand");
    return super.onStartCommand(intent, flags, startId);

}
public void onDestroy() {
    super.onDestroy();
    Log.d("MyService", "destroyed");
}

}

  • 写回答

1条回答 默认 最新

  • 我叫Tomes派大星 2016-04-21 06:04
    关注

    贴Log看看,log打印的对吗?

    评论

报告相同问题?

悬赏问题

  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境