registerReceiver(...)传递ClickListener

时间:2014-04-15 18:59:19

标签: android bluetooth

我在传递正确的信息以设置蓝牙发现和配对方面遇到了一些困难。我一直非常密切地关注Android开发人员示例文档,并且无法弄清楚我需要传递给此方法的内容。这是我一直在使用的网站,供参考。

http://developer.android.com/guide/topics/connectivity/bluetooth.html

TransferFragment:

//some code used from 
//   http://examples.javacodegeeks.com/android/core/ui/progressdialog/android-progressdialog-example/
//   http://developer.android.com/guide/topics/connectivity/bluetooth.html

package com.project.BluetoothTransfer_v1000;

import java.util.Set;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class TransferFragment extends Fragment{

    private TextView filePathTextView;
    private Button startTransferButton;
    private ImageView bluetoothImage;
    ProgressDialog transferDialog;
    Handler updateBarHandler;
    private static final int REQUEST_BLUETOOTH = 1;
    private static final int DISCOVER_DURATION = 300;
    Context context;
    ArrayAdapter mArrayAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, final Bundle savedInstanceState){

        super.onCreate(savedInstanceState);

        //set the user interface layout for this activity
        setRetainInstance(false);
        View v = inflater.inflate(R.layout.activity_bluetooth_transfer, parent, false);

        context = this.getActivity();
        filePathTextView = (TextView) v.findViewById(R.id.file_path_textView);
        startTransferButton = (Button) v.findViewById(R.id.start_transfer_button);
        bluetoothImage = (ImageView) v.findViewById(R.id.bluetooth_imageView);
        bluetoothImage.setClickable(true);

        startTransferButton.setOnClickListener(new View.OnClickListener() {
            //start transfer processes
            @SuppressWarnings("unchecked")
            @Override
            public void onClick(View v){
                //check to make sure the file path text view != null
                BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
                int REQUEST_ENABLE_BT = -1;
                //ensure the device being used has bluetooth capability
                if (btAdapter != null){
                    //check-enable bluetooth
                    if (!btAdapter.isEnabled()) {
                        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
                        }
                    //ensure the textview isn't empty
                    //check if filepath is null
                    if (filePathTextView.getText().toString().length() != 0){
                        Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();
                        //check if there are paired devices
                        if (pairedDevices.size() > 0){
                            //loop through paired devices
                            for (BluetoothDevice device: pairedDevices){
                                //add the name and address to an array adapter to show in a ListView
                                mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
                            }                   
                        }//end found paired devices if

                        // Create a BroadcastReceiver for ACTION_FOUND
                        final BroadcastReceiver mReceiver = new BroadcastReceiver() {
                            public void onReceive(Context context, Intent intent) {
                                String action = intent.getAction();
                                // When discovery finds a device
                                if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                                    // Get the BluetoothDevice object from the Intent
                                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                                    // Add the name and address to an array adapter to show in a ListView
                                    mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
                                }
                            }
                        };
                        // Register the BroadcastReceiver
                        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);

                        //COMPILER ERROR BELOW @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                        //"This method registerReceiver(BroadcastReceiver, IntentFilter)
                        //is undefined for type new View.OnClickListener
                        registerReceiver(mReceiver, filter); 

                        Intent discoverableIntent = new
                        Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
                        discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
                        startActivity(discoverableIntent);
                        btAdapter.cancelDiscovery();
                    }//
                    else {
                        //alert user to input file path
                        new AlertDialog.Builder(context).setTitle("Error")
                        .setMessage("Please insert a filename to send.")
                        .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {}
                        }).show();
                    }//ifnull (else)
                }//bt equipped check
            }//end anon class
        });

        bluetoothImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //display dialog showing program specs and creators
                new AlertDialog.Builder(context).setTitle("About")
                .setMessage("Created by:"+"\n"+ "Hal, Chris, and Roger")
                .setPositiveButton("Awesome!", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                }).show();
            }
        });
        return v;
    }
}

TransferActivity:

package com.project.BluetoothTransfer_v1000;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

public class TransferActivity extends FragmentActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transfer);
        TransferFragment fragment = new TransferFragment();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.container, fragment);
        fragmentTransaction.commit();
    }
}

1 个答案:

答案 0 :(得分:0)

尝试:

TransferFragment.this.getActivity().registerReceiver(mReceiver, filter); 

而不是:

registerReceiver(mReceiver, filter); 

因为方法registerReceiver是Activity的一部分而不是OnClickListener。这就是为什么错误消息抱怨OnClickListener中找不到找到的方法:

  

此方法registerReceiver(BroadcastReceiver,IntentFilter)   未定义类型为新的View.OnClickListener