Reputation: 633
Why cannot resolve this method in a class? This class is out off main android class. If I put the method in main class work fine, but not in other class.
class blu {
public blu(){
}
public boolean comprobarBluetooth(Context context){
CharSequence text = "No tiene Bluetooth";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
//mirem si hi ha bluetooth al aparell
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter==null) {
//disparo un toast per informar
return false;
}else{
if (!mBluetoothAdapter.isEnabled()) {
//si està apagat obrim el dialeg per activarlo.
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}//SI startActivityForResult torna un OK faig return true, i executo el metode de buscar dispositivos
}
return true;
}
Upvotes: 0
Views: 3441
Reputation: 2626
If context
is an instance of Activity you can use:
((Activity) context).startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
If it is not an instance of Activity you will get an exception.
Upvotes: 5
Reputation: 1006664
startActivityForResult()
is a method on Activity
. You cannot call startActivityForResult()
from some random Java class. You need to call startActivityForResult()
on an Activity
, specifically the activity where you plan on getting the results via onActivityResult()
.
Upvotes: 4
Reputation: 2644
startActivityForResult
is a method of the Context
class. You will have to call context.startActivityForResult(...)
.
Upvotes: -2