android Xamarin安卓系统:我的[广播接收器]坏了

oknwwptz  于 2022-12-25  发布在  Android
关注(0)|答案(1)|浏览(187)

我试图写一个程序在“Xamarin C# Android”读取纬度和经度从GPS和显示在TextView。
程序运行时没有任何错误,但TextView中也没有任何内容,因为我制作的Broadcast Receiver根本不工作,也就是说,当我在OnReceive()函数中放置断点时,它根本不运行。
另外,我到处都读到编译器应该在Android清单文件中为它添加一行,但在我的AndroidManifest.xml中什么也没有添加。

using Android.App;
using Android.OS;
using Android.Runtime;
using Android.Widget;
using AndroidX.AppCompat.App;
using Android.Content;
using AndroidX.Core.App;
using System;
using Android;


namespace EhsanSensors
{

  

    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {
        TextView _locationText;
        TextView _addressText;
        TextView _remarksText;

        GPSServiceBinder _binder;
        GPSServiceConnection _gpsServiceConnection;
        Intent _gpsServiceIntent;
        private GPSServiceReciever _receiver;
        public static MainActivity Instance;


        [BroadcastReceiver(Enabled = true, Exported = true)]
        [IntentFilter(actions: new[] { Intent.ActionBootCompleted })]

        public class GPSServiceReciever : BroadcastReceiver
        {
            public static readonly string LOCATION_UPDATED = "LOCATION_UPDATED";
            public override void OnReceive(Context context, Intent intent)
            {
                if (intent.Action.Equals(LOCATION_UPDATED))
                {
                    MainActivity.Instance.UpdateUI(intent);
                }

            }
        }

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            // Set our view from the "main" layout resource
            Instance = this;
            SetContentView(Resource.Layout.activity_main);

            _addressText = FindViewById<TextView>(Resource.Id.txtAddress);
            _locationText = FindViewById<TextView>(Resource.Id.txtLocation);
            _remarksText = FindViewById<TextView>(Resource.Id.txtRemarks);

            ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.AccessFineLocation }, 1);
            RegisterBroadcastReceiver();
            RegisterService();

            
        }
        private void RegisterService()
        {
            _gpsServiceConnection = new GPSServiceConnection(_binder);
            _gpsServiceIntent = new Intent(Android.App.Application.Context, typeof(GPSService));
            BindService(_gpsServiceIntent, _gpsServiceConnection, Bind.AutoCreate);
        }
        private void RegisterBroadcastReceiver()
        {
            IntentFilter filter = new IntentFilter(GPSServiceReciever.LOCATION_UPDATED);
            filter.AddCategory(Intent.CategoryDefault);
            _receiver = new GPSServiceReciever();
            RegisterReceiver(_receiver, filter);
        }
        protected override void OnResume()
        {
            base.OnResume();
            RegisterBroadcastReceiver();
        }

        protected override void OnPause()
        {
            base.OnPause();
            UnRegisterBroadcastReceiver();
        }

       private void UnRegisterBroadcastReceiver()
        {
            UnregisterReceiver(_receiver);
        }
        public void UpdateUI(Intent intent)
        {
            _locationText.Text = intent.GetStringExtra("Location");
            _addressText.Text = intent.GetStringExtra("Address");
            _remarksText.Text = intent.GetStringExtra("Remarks");
        }

        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }



}

对广播接收机使用这些代码:

[BroadcastReceiver(Enabled = true, Exported = true)]

[BroadcastReceiver]

[IntentFilter(actions: new[] { Intent.ActionBootCompleted })]

或不使用IntentFilter

nsc4cvqm

nsc4cvqm1#

[IntentFilter(actions: new[] { Intent.ActionBootCompleted })]

Onreceive()方法将在手机开机时调用,因此需要重新启动手机。
您是否申请过位置权限?如果没有,需要动态申请。
要了解更多信息,可以查看Broadcast Receivers in Xamarin.Android

相关问题