2016-04-19 16 views
0

クラス内でこのメソッドを解決できないのはなぜですか?このクラスは主なアンドロイドクラスから外れています。私は主なクラスのメソッドは、他のクラスではなく、うまく動作します。このコードのメソッドstartActivityforresultsを解決できません

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; 
     } 

答えて

3

startActivityForResult()は、a method on Activityです。任意のJavaクラスからstartActivityForResult()を呼び出すことはできません。 ActivitystartActivityForResult()に電話する必要があります。具体的には、onActivityResult()で結果を得る予定です。

-1

startActivityForResultは、Contextクラスのメソッドです。 context.startActivityForResult(...)に電話する必要があります。

1

contextた場合には、使用可能な活動のインスタンスである:

((Activity) context).startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 

それが活動のインスタンスでない場合は、例外が発生します。

関連する問題