从服务调用 setResult class
calling setResult from Service class
在我的 UiFragment
程序中,我尝试连接我的设备。
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CONNECT_DEVICE_SECURE:
// When DeviceListActivity returns with a device to connect
if (resultCode == Activity.RESULT_OK) {
connectDevice(data);
}
break;
}
}
我也有 StartScanService
class 需要做类似于 setResult
的事情(就像我在 Activity class 中做的那样)
public class StartScanService extends Service {
...
...
private void connect(String tmp){
adapter.cancelDiscovery();
String address = tmp.substring(tmp.length() - 17);
// Create the result Intent and include the MAC address
Intent intent = new Intent();
intent.putExtra(EXTRA_DEVICE_ADDRESS, address);
// Set result
//setResult(Activity.RESULT_OK, intent);
}
}
所以我需要从服务 class 调用片段 class 方法。可以吗?
我使用界面来做到这一点。首先我创建界面
public interface ServiceCallBack {
void connectToDevice(Intent intent);
}
在我的服务中 class 我调用接口方法 'connectToDevice'
public class StartScanService extends Service {
...
...
...
private ServiceCallBack serviceCallBack;
private void connect(String tmp){
adapter.cancelDiscovery();
String address = tmp.substring(tmp.length() - 17);
// Create the result Intent and include the MAC address
Intent intent = new Intent();
intent.putExtra(EXTRA_DEVICE_ADDRESS, address);
if (serviceCallBack != null) {
serviceCallBack.connectToDevice(intent);
}
}
}
我的 Fragment class 必须实现 ServiceCallBack 接口
public class UiFragment extends Fragment implements ServiceCallBack{
private ServiceConnection connectionService = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(TAG + " VEG", "onServiceConnected");
StartScanService.ScanBinder scan = (StartScanService.ScanBinder) service;
startScanService = scan.getScanService();
startScanService.setHandler(handler);
startScanService.setServiceCallBack(UiFragment.this);
startScanService.startScan();
bound = true;
}
@Override
public void onServiceDisconnected(ComponentName name) {
bound = false;
Log.d(TAG, "onServiceDisconnected");
}
};
@Override
public void connectToDevice(Intent intent) {
connectDevice(intent);
}
}
在我的 UiFragment
程序中,我尝试连接我的设备。
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CONNECT_DEVICE_SECURE:
// When DeviceListActivity returns with a device to connect
if (resultCode == Activity.RESULT_OK) {
connectDevice(data);
}
break;
}
}
我也有 StartScanService
class 需要做类似于 setResult
的事情(就像我在 Activity class 中做的那样)
public class StartScanService extends Service {
...
...
private void connect(String tmp){
adapter.cancelDiscovery();
String address = tmp.substring(tmp.length() - 17);
// Create the result Intent and include the MAC address
Intent intent = new Intent();
intent.putExtra(EXTRA_DEVICE_ADDRESS, address);
// Set result
//setResult(Activity.RESULT_OK, intent);
}
}
所以我需要从服务 class 调用片段 class 方法。可以吗?
我使用界面来做到这一点。首先我创建界面
public interface ServiceCallBack {
void connectToDevice(Intent intent);
}
在我的服务中 class 我调用接口方法 'connectToDevice'
public class StartScanService extends Service {
...
...
...
private ServiceCallBack serviceCallBack;
private void connect(String tmp){
adapter.cancelDiscovery();
String address = tmp.substring(tmp.length() - 17);
// Create the result Intent and include the MAC address
Intent intent = new Intent();
intent.putExtra(EXTRA_DEVICE_ADDRESS, address);
if (serviceCallBack != null) {
serviceCallBack.connectToDevice(intent);
}
}
}
我的 Fragment class 必须实现 ServiceCallBack 接口
public class UiFragment extends Fragment implements ServiceCallBack{
private ServiceConnection connectionService = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(TAG + " VEG", "onServiceConnected");
StartScanService.ScanBinder scan = (StartScanService.ScanBinder) service;
startScanService = scan.getScanService();
startScanService.setHandler(handler);
startScanService.setServiceCallBack(UiFragment.this);
startScanService.startScan();
bound = true;
}
@Override
public void onServiceDisconnected(ComponentName name) {
bound = false;
Log.d(TAG, "onServiceDisconnected");
}
};
@Override
public void connectToDevice(Intent intent) {
connectDevice(intent);
}
}