2017-07-12 10 views
0

AlertDialog.BuilderActivityで呼び出されるメソッド内で使用したいのですが、このメソッドはServiceの内部に定義されていますので、これとActivityなどは動作していないようです。 :AlertDialog.Builderのコンストラクタに適切なコンテキストを取得するにはどうすればよいですか?

public class BluetoothLeService extends Service { 
... 
    public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) 
      AlertDialog.Builder builder = new AlertDialog.Builder(CONTEXT); 
      builder.setMessage("Write the CCCD Descriptor?").setPositiveButton("Yes", dialogClickListener) 
       .setNegativeButton("No", dialogClickListener).show(); 

    } 
} 

あなたは上で見たこの方法は、中に呼び出されます:のLEA、今、私が取得するための適切なコンテキストが何であるかを知らない

public class DeviceControlActivity extends Activity { 

    private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() { 
     public void onReceive(Context context, Intent intent) { 
       ... 
       if ((UserValueCharacteristic.PROPERTY_NOTIFY) > 0) { 
          mNotifyCharacteristic = UserValueCharacteristic; 
          mBluetoothLeService.setCharacteristicNotification(
          UserValueCharacteristic, true); 
       } 

      } 
     } 

}

それらを得る方法。

this (return unable to add window token null error during run time) 

DeviceControlActivity.this (won't compile, saying class is non-closing) 

DeviceControlActivity.getapplication() 

DeviceControlActivity.getapplication().this 

およびそれらのこれまでのところどれも:私はAlertDialog.Builderだけ活動Contextを取ることを決定し、最も可能性の高いActivityContextそれは取得する必要がDeviceControlActivityに属し、私が試した少し研究して

働いた。

これはアンドロイドAPIの設計上の欠陥のように感じ始めますが、これは本当に不合理です。ポップアップメッセージはあんなに面倒ではありません。

誰かお手伝いできますか?

+0

Applicationクラスを拡張するクラスを追加し、そののonCreateメソッドでそのオブジェクトをインスタンス化活動のコンテキストを渡す活動からメソッドを呼び出しますクラス。今すぐあなたのサービスでこのようなコンテキストを取得するMyApplicationClass.getInstance()。getApplicationContext(); –

答えて

0

so this and activity.this etc doesn't appear to be working

Serviceので、それは問題ではありませんContextを拡張します。あなたが直面している問題は、AlertDialog.Builderに関連していない可能性があります。

あなたにも試すことができます。

public class BluetoothLeService extends Service { 
... 
    public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) 
      AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext()); 
      builder.setMessage("Write the CCCD Descriptor?").setPositiveButton("Yes", dialogClickListener) 
       .setNegativeButton("No", dialogClickListener).show(); 

    } 
} 

編集:

利用放送受信機によって与える文脈あなたがする必要がある唯一のものは、コンテキストを渡すことです

public class BluetoothLeService extends Service { 
... 
    public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled, Context context) 
      AlertDialog.Builder builder = new AlertDialog.Builder(context); 
      builder.setMessage("Write the CCCD Descriptor?").setPositiveButton("Yes", dialogClickListener) 
       .setNegativeButton("No", dialogClickListener).show(); 

    } 
} 


public class DeviceControlActivity extends Activity { 

    private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() { 
     public void onReceive(Context context, Intent intent) { 
       ... 
       if ((UserValueCharacteristic.PROPERTY_NOTIFY) > 0) { 
          mNotifyCharacteristic = UserValueCharacteristic; 
          mBluetoothLeService.setCharacteristicNotification(
          UserValueCharacteristic, true, context); 
       } 

      } 
     } 
+0

こんにちは、前に試したgetApplicationContext、いいえjoy :(@Cochi –

+0

私の編集が役立つかどうかを見てください:) – Cochi

+0

こんにちは、私はそれを試しました。 –

0

方法に。ここでは一例方法です。

public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled, Context context) 
     AlertDialog.Builder builder = new AlertDialog.Builder(context); 
     builder.setMessage("Write the CCCD Descriptor?").setPositiveButton("Yes", dialogClickListener) 
      .setNegativeButton("No", dialogClickListener).show(); 

} 

とあなただけの

if ((UserValueCharacteristic.PROPERTY_NOTIFY) > 0) { 
         mNotifyCharacteristic = UserValueCharacteristic; 
         mBluetoothLeService.setCharacteristicNotification(
         UserValueCharacteristic, true, myActivity.Context); 
} 
+0

こんにちは@リカルド、私は文脈に入れて、それは問題はないようですが、あなたのアドバイスに従って、私はこれを入れ、それは誤りを暗示しました。見つかった: 'android.content.BroadcastReceiver'、必須: 'android.content.context' –

+0

これはどのようなエラーですか? – Ricardo

+0

とコンテキストを使用するとき、私は質問で述べたように、もう一度悪い例外エラーを受け取りました。 –

関連する問題