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

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打印的对吗?

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误