如何为构造函数AlertDialog.Builder获取正确的上下文?

时间:2017-07-12 09:18:54

标签: java android

我想在方法中使用<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id=container></div>,该方法将由AlertDialog.Builder调用,问题是,此方法是在Activity内定义的,所以这和{{1} } .this似乎没有用,这里是删节代码:

Service

您在上面看到的这种方法将在:

中调用
Activity

}

现在我不知道获得什么是正确的背景,至少不知道如何获得它们。

通过一些研究,我决定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(); } } 只会采取活动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); } } } ,并且它应该最有可能AlertDialog.Builder Context属于Activity,我试过了:

Context
到目前为止,他们都没有工作。

这开始感觉像是android API的设计缺陷,这真的是不合理的。弹出消息不应该是一个麻烦。

有人可以帮帮我吗?

2 个答案:

答案 0 :(得分:0)

  

所以这个和activity.this等似乎没有工作

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

           }
      }

答案 1 :(得分: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);
}