-1

我正在尝试将用户从微调器中选择它的活动中的时间值传递给服务,以便音频剪辑将播放该时间长度。到目前为止,我没有收到任何错误,但该服务没有在请求的时间内播放。我没有发布 logcat,因为我没有错误,我假设这是我忽略的简单事情。我想从活动中获取 TIME_IN_MINUTES 值并让服务播放那么长时间。

服务等级

@Override
public void onStart(Intent intent, int startid) {
    long time = 0;
    if (intent.hasExtra("TIME_IN_MINUTESC")) {
        time = intent.getLongExtra("TIME_IN_MINUTES", 1000);
    }
    Toast.makeText(this, "Ship Service Started for" + time, 
Toast.LENGTH_LONG).show();
    Log.d(TAG, "onStart");
    myPlayer.start();
}
}

活动课。

  @Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.ship);
    button2 = (Button) findViewById(R.id.btn2);
    button2.setOnClickListener(this);
    stop1 = (Button) findViewById(R.id.stop);
    stop1.setOnClickListener(this);
    spinner2 = (Spinner) findViewById(R.id.spinner2);
    ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this,
    android.R.layout.simple_spinner_item, TIME_IN_MINUTES);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(adapter);
    Intent intService =  new Intent(Ship.this, Shipservice.class);
    intService.putExtra("TIME_IN_MINUTES", 1000);

}

// Handle button callback
@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btn2:
        Log.d(TAG, "onClick: starting service");
        startService(new Intent(this, Shipservice.class));
        break;
    case R.id.stop:
        Log.d(TAG, "onClick: stopping srvice");
        stopService(new Intent(this, Shipservice.class));
        break;
    }
4

2 回答 2

1

你还没有Service像这样启动你的启动服务..

Intent intService =  new Intent(Ship.this, Shipservice.class);
intService.putExtra("TIME_IN_MINUTES", 1000);
startService(intService);

在按钮中onClick,您已经启动了服务,但该 Intent 没有输入任何值。Service以相同的方式开始Intnet或将数据添加到新的Intnet.

或者像这样简单地改变这种方法..

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btn2:
        Log.d(TAG, "onClick: starting service");
        startService(intService);
        break;
    case R.id.stop:
        Log.d(TAG, "onClick: stopping srvice");
        stopService(intService);
        break;
    }

intservice其作为私有变量..

于 2013-11-13T06:29:02.477 回答
0

在活动课上试试这个

intent = new Intent(this, Service.class);
    intent.putExtra("key", resultReceiver);

    startService(intent);
}

private final ResultReceiver resultReceiver = new ResultReceiver(
        new Handler()) {
    @SuppressWarnings("unchecked")
    @Override
    protected void onReceiveResult(int resultCode, Bundle resultData) {
        progressBar.setVisibility(View.GONE);
        //do functionality
    };
};

在服务级别;-

@Override
protected void onHandleIntent(Intent intent) {
    // TODO Auto-generated method stub
    // TODO Auto-generated constructor stub

           //do functionality


    Bundle bundle = new Bundle();
    bundle.putSerializable("key", "data to send");
    ResultReceiver receiver = intent.getParcelableExtra("key");
    receiver.send(0, bundle);

}
于 2013-11-13T06:37:27.010 回答