有没有办法调用onReceive?

时间:2016-06-20 00:56:25

标签: android broadcastreceiver

如何从onCreate调用我的onReceive?我不想在我的清单中注册这个接收器。有没有解决方法?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Animation = findViewById(R.id.shadow_candy);
    mTimer = (TextView) findViewById(R.id.timer);
    BA = BluetoothAdapter.getDefaultAdapter();
    if (!BA.isEnabled()) {
        Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(turnOn, 0);
    }
    // ...
    IntentFilter filter1 = new IntentFilter(
            BluetoothDevice.ACTION_ACL_CONNECTED);
    IntentFilter filter2 = new IntentFilter(
            BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
    IntentFilter filter3 = new IntentFilter(
            BluetoothDevice.ACTION_ACL_DISCONNECTED);
    this.registerReceiver(BTReceiver, filter1);
    this.registerReceiver(BTReceiver, filter2);
    this.registerReceiver(BTReceiver, filter3);
}

// The BroadcastReceiver that listens for bluetooth broadcasts
private final BroadcastReceiver BTReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
            // Do something if connected
            Toast.makeText(getApplicationContext(), "BT Connected",
                    Toast.LENGTH_SHORT).show();
            // change lower button
            findViewById(R.id.connect).setVisibility(View.GONE);
            findViewById(R.id.connected).setVisibility(View.VISIBLE);
            // change upper button
            stopfinding();
            findViewById(R.id.find_me).setVisibility(View.GONE);
            findViewById(R.id.disconnect).setVisibility(View.VISIBLE);
            // call my endless glowing animation loop
            method1();
        } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
            // Do something if disconnected
            Toast.makeText(getApplicationContext(), "BT Disconnected",
                    Toast.LENGTH_SHORT).show();
            // change lower button
            findViewById(R.id.connect).setVisibility(View.VISIBLE);
            findViewById(R.id.connected).setVisibility(View.GONE);
            // change upper button
            findViewById(R.id.find_me).setVisibility(View.VISIBLE);
            findViewById(R.id.disconnect).setVisibility(View.GONE);
            // end animation
            findViewById(R.id.shadow_candy).setVisibility(View.GONE);
            handler.removeCallbacks(runnable);
        }
        // else if...
    }
};

我已经尝试了这个,但是没有用

    Intent intent = new Intent();
    sendBroadcast(intent);

2 个答案:

答案 0 :(得分:0)

你的意图是什么都不发送。

/**
 * Create an empty intent.
 */
public Intent() {
}

它没有来源(上下文),没有目的地,没有行动。所以我认为它无法收到。

要修复它,请使用Intent类的另一个构造函数:

Intent with Action

public Intent(String action)

Intent with destination

public Intent(Context packageContext, Class<?> cls)

答案 1 :(得分:0)

  

我已经尝试了这个,但是不起作用

web.config

发送广播接收器时,应指定意图过滤器

Intent intent = new Intent();
sendBroadcast(intent);

IntentFilter filter1 = new IntentFilter(
        BluetoothDevice.ACTION_ACL_CONNECTED);
Intent intent = new Intent(filter1);
sendBroadcast(intent);

然后使用指定的意图过滤器注册您的接收器

Intent intent = new Intent("my_intent_filter");
sendBroadcast(intent);

this.registerReceiver(BTReceiver, filter1);