我有下一个代码:
class Printer{
Activity activity;
public Printer (Activity activity) {
this.activity = activity;
initializeBluetooth();
}
public boolean initializeBluetooth() {
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
Context context = activity.getApplicationContext();
Toast toast = Toast.makeText(context, activity.getString(R.string.notSupportedBluetooth), 3000);
toast.show();
return false;
} else {
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
activity.startActivityForResult(enableBtIntent, 0);
return false;
}
}
return true;
}
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
System.out.println(resultCode);
if (resultCode == 0) {
System.out.println("one");
} else {
System.out.println("two");
}
}}
问题是onActivityResult,我知道它不能被调用,因为该类不是一个Activity,所以我如何检查用户是否点击了蓝牙请求的是或否,而不是将我的类转为活动? ? activity.startActivityForResult(enableBtIntent, 0);
谢谢
答案 0 :(得分:2)
你为什么要这样做?只需在Activity
类中包含所有蓝牙功能......无需为此创建单独的类。
无论如何,只需将onActivityResult
代码复制并粘贴到Activity
文件中即可:
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
System.out.println(resultCode);
if (resultCode == 0) {
System.out.println("one");
} else {
System.out.println("two");
}
}
在onActivityResult
课程中加入Printer
是没有意义的,因为调用onActivityResult
会调用Activity
。