如何在Activity类中创建一个Broadcast接收器类

时间:2018-01-08 16:37:59

标签: c# android xamarin xamarin.forms mobile-application

我尝试使用“活动”从广播接收器更新扫描的蓝牙设备列表,在c#中执行类似下面的代码。

但我将我的广播接收器作为一个单独的类我所以无法将扫描的设备添加到列表中。

你能否建议我使用c#xamarin更好的方法。

我试图通过广播接收器与活动进行沟通

public class DeviceListFragment extends Fragment implements AbsListView.OnItemClickListener{
...
private final BroadcastReceiver bReciever = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            // Create a new device item
            DeviceItem newDevice = new DeviceItem(device.getName(), device.getAddress(), "false");
            // Add it to our adapter
            mAdapter.add(newDevice);
        }
    }
};

}

这是我试过的代码。

命名空间启动 {

[Activity(Label = "Launch", MainLauncher = true)]
public class MainActivity : Activity
{
    string[] items;
    BluetoothAdapter bluetoothadapter;
    ListView listView;
    //BroadcastReceiver broadcast=null;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        items = new string[] { "wec","wce","dddsa","wecwe"};
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

listView = (ListView) FindViewById<ListView>(Resource.Id.devices_lstv);

        ArrayAdapter arrayAdapter = new ArrayAdapter<string>(this,Android.Resource.Layout.SimpleExpandableListItem1,items);
        listView.Adapter=arrayAdapter;


    RegisterReceiver(new BTimeReceiver(this), new IntentFilter(BluetoothDevice.ActionFound));
    }



    class BTimeReceiver : BroadcastReceiver
    {
        MainActivity activity;
        public BTimeReceiver(MainActivity activity) : base()
        {
            this.activity = activity;
        }

        public override void OnReceive(Context context, Intent intent)
        {

            Toast.MakeText(context, "Received bluetooth device!", ToastLength.Short).Show();
            BluetoothDevice device = (Android.Bluetooth.BluetoothDevice)intent.GetParcelableExtra(BluetoothDevice.ExtraDevice);

            Toast.MakeText(context, device.Name, ToastLength.Short).Show();

            // access your service via the "service" var...
        }
    }

}

}

这是从头开始的错误

Java.Lang.ClassNotFoundException:在路径上找不到类“md5595308ae13c09d60df4f8b95233e7c94.MainActivity_BTimeReceiver”:DexPathList [[zip file“/data/app/Launch.Launch-1/base.apk"],nativeLibraryDirectories=[/data /app/Launch.Launch-1/lib/arm,/data/app/Launch.Launch-1/base.apk!/lib/armeabi-v7a,/ vendor / lib,/ system / lib]]   在Java.Interop.JniEnvironment + Types.FindClass(System.String classname)[0x00114] in&lt; 54816278eed9488eb28d3597fecd78f8&gt ;:0   在Java.Interop.JniType..ctor(System.String classname)[0x00006] in&lt; 54816278eed9488eb28d3597fecd78f8&gt ;:0   在Java.Interop.JniPeerMembers + JniInstanceMethods..ctor(System.Type declaringType)[0x00064] in&lt; 54816278eed9488eb28d3597fecd78f8&gt ;:0   在Java.Interop.JniPeerMembers + JniInstanceMethods.GetConstructorsForType(System.Type declaringType)[0x0002c] in&lt; 54816278eed9488eb28d3597fecd78f8&gt ;:0   at Java.Interop.JniPeerMembers + JniInstanceMethods.StartCreateInstance(System.String constructorSignature,System.Type declaringType,Java.Interop.JniArgumentValue * parameters)[0x00032] in&lt; 54816278eed9488eb28d3597fecd78f8&gt ;:0   在Android.Content.BroadcastReceiver..ctor()[0x00034]中:0   在Launch.MainActivity + BTimeReceiver..ctor(Launch.MainActivity mainActivity)[0x00000]在C:\ Users \ ACHINTAR \ source \ repos \ Launch \ Launch \ MainActivity.cs:73   在发布时

1 个答案:

答案 0 :(得分:0)

  

java.lang.ClassNotFoundException:在路径上找不到类...:DexPathList

这是创建Java classes.zip文件时的一个已知问题:

解决方法:

您可以删除项目的bin&amp; obj文件并清理/重建作为解决方法,以便重新创建Java classes.zip文件。