Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在BroadcastReceiver中使用EventListener和Xamarin.Android中的自定义操作?_Android_Xamarin.android_Broadcastreceiver - Fatal编程技术网

如何在BroadcastReceiver中使用EventListener和Xamarin.Android中的自定义操作?

如何在BroadcastReceiver中使用EventListener和Xamarin.Android中的自定义操作?,android,xamarin.android,broadcastreceiver,Android,Xamarin.android,Broadcastreceiver,当BroadcastReceiver收到自定义消息时,我正试图启动ticketvent 例如,当intent.Action==intent.ActionTimeTick时(而不是GRID\u start),它工作正常。 我注释掉了工作代码,因此您可以轻松地进行实验 看来我错过了一件关于行动的重要事情。你能帮我一下吗 using System; using Android.App; using Android.Content; using Android.OS; using Android.Run

BroadcastReceiver
收到自定义消息时,我正试图启动
ticketvent

例如,当
intent.Action==intent.ActionTimeTick
时(而不是
GRID\u start
),它工作正常。 我注释掉了工作代码,因此您可以轻松地进行实验

看来我错过了一件关于行动的重要事情。你能帮我一下吗

using System;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Support.Design.Widget;
using Android.Support.V7.App;
using Android.Util;
using Android.Views;
using Android.Widget;

namespace TestApp
{
    [Activity(Label = "BroadcastUpdateFormExample", MainLauncher = true)]
    public class MainActivity : Activity
    {
        int count = 1;
        TextView txtNote;
        BroadcastMessageCatcher catcher;
        static readonly string TAG = "MainActivity";

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);

            txtNote = FindViewById<TextView>(Resource.Id.textView1);

            Button button = FindViewById<Button>(Resource.Id.button1);

            button.Click += delegate
            {
                button.Text = string.Format("{0} clicks!", count++);
                BroadcastStarted();
            };

            catcher = new BroadcastMessageCatcher();
            catcher.TickEvent += delegate
            {
                Log.Debug(TAG, "before listener");
                txtNote.Text = "Tick";
                Log.Debug(TAG, "after listener");
            };
        }

        protected override void OnResume()
        {
            base.OnResume();
            // RegisterReceiver (catcher, new IntentFilter (Intent.ActionTimeTick));
            IntentFilter filter = new IntentFilter(BroadcastMessageCatcher.GRID_STARTED);
            RegisterReceiver(catcher, filter);
        }

        protected override void OnPause()
        {
            base.OnPause();
            UnregisterReceiver(catcher);
        }

        private void BroadcastStarted()
        {
            // Broadcast message to the view
            // Source: https://forums.xamarin.com/discussion/1147/updating-activity-using-a-background-service
            Intent BroadcastIntent = new Intent(this, typeof(BroadcastMessageCatcher));
            BroadcastIntent.SetAction(BroadcastMessageCatcher.GRID_STARTED);
            //BroadcastIntent.AddCategory(Intent.CategoryDefault);
            Log.Debug(TAG, "Broadcast started");
            SendBroadcast(BroadcastIntent);
        }
    }



    [BroadcastReceiver]
    public class BroadcastMessageCatcher : BroadcastReceiver
    {
        static readonly string TAG = "MainActivity";
        public static readonly string GRID_STARTED = "GRID_STARTED";
        public override void OnReceive(Context context, Intent intent)
        {
            // if (intent.Action == Intent.ActionTimeTick) 
            if (intent.Action == GRID_STARTED)
            {
                Log.Debug(TAG, "before event");
                // Here I get the error:
                TickEvent();
                Log.Debug(TAG, "after event");
            }
        }

        public delegate void TickEventHandler();
        public event TickEventHandler TickEvent;
    }
}
使用系统;
使用Android.App;
使用Android.Content;
使用Android.OS;
使用Android.Runtime;
使用Android.Support.Design.Widget;
使用Android.Support.V7.App;
使用Android.Util;
使用Android.Views;
使用Android.Widget;
命名空间TestApp
{
[活动(Label=“BroadcastUpdateFormExample”,MainLauncher=true)]
公共课活动:活动
{
整数计数=1;
文本视图txtNote;
广播消息捕捉器捕捉器;
静态只读字符串TAG=“MainActivity”;
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
//从“主”布局资源设置视图
SetContentView(Resource.Layout.activity_main);
txtNote=findviewbyd(Resource.Id.textView1);
Button Button=FindViewById(Resource.Id.button1);
按钮。单击+=委派
{
button.Text=string.Format(“{0}点击!”,count++);
广播开始();
};
catcher=新广播消息catcher();
catcher.ticketvent+=委托
{
调试(标记为“侦听器之前”);
txtNote.Text=“勾选”;
调试(标记为“侦听器之后”);
};
}
受保护的覆盖void OnResume()
{
base.OnResume();
//RegisterReceiver(catcher,newintentfilter(Intent.ActionTimeTick));
IntentFilter=newintentfilter(BroadcastMessageCatcher.GRID\u已启动);
寄存器接收器(捕集器、过滤器);
}
受保护的覆盖void OnPause()
{
base.OnPause();
未注册接收器(捕获器);
}
私有void广播已启动()
{
//向视图广播消息
//资料来源:https://forums.xamarin.com/discussion/1147/updating-activity-using-a-background-service
Intent BroadcastIntent=新的Intent(这个,类型为(BroadcastMessageCatcher));
BroadcastIntent.SetAction(BroadcastMessageCatcher.GRID_已启动);
//BroadcastIntent.AddCategory(Intent.CategoryDefault);
调试(标记“广播已启动”);
发送广播(广播意图);
}
}
[广播接收机]
公共类BroadcastMessageCatcher:BroadcastReceiver
{
静态只读字符串TAG=“MainActivity”;
公共静态只读字符串GRID\u start=“GRID\u start”;
公共覆盖void OnReceive(上下文、意图)
{
//if(intent.Action==intent.ActionTimeTick)
if(intent.Action==网格_已启动)
{
调试(标记“事件前”);
//这里我得到了一个错误:
ticketvent();
调试(标记“事件后”);
}
}
公共委托void TickEventHandler();
公共活动票证;票证;
}
}

我在
ticketvent()上收到“System.NullReferenceException:'对象引用未设置为对象的实例'”错误
广播消息捕捉器中(在注释中标记)。

我理解操作是错误的。主要是因为我不知道引发活动的意图和引发行动的意图是两件完全不同的事情

因此,如果它对任何人都有帮助,我会发布工作代码

主要测试:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
          android:versionCode="1" 
          android:versionName="1.0" 
          package="com.companyname.testapp">
  <uses-sdk android:minSdkVersion="28" android:targetSdkVersion="28" />
  <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme">
    <activity
     android:name=".MainActivity">
     <intent-filter>
         <action android:name="com.companyname.testapp.MY_CUSTOM_ACTION"></action>
         <category android:name="android.intent.category.DEFAULT"></category>
     </intent-filter>
    </activity>
  </application>
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>

活动:

using Android.App;
using Android.Content;
using Android.OS;
using Android.Util;
using Android.Widget;

namespace TestApp
{
    [Activity(Label = "BroadcastUpdateFormExample", MainLauncher = true)]
    public class MainActivity : Activity
    {
        int count = 1;
        TextView txtNote;
        BroadcastMessageCatcher catcher;
        static readonly string TAG = "FCMActivity";

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);

            txtNote = FindViewById<TextView>(Resource.Id.textView1);

            Button button = FindViewById<Button>(Resource.Id.button1);

            button.Click += delegate
            {
                button.Text = string.Format("{0} clicks!", count++);
                BroadcastStarted();
            };

            catcher = new BroadcastMessageCatcher();
            catcher.TickEvent += delegate
            {
                Log.Debug(TAG, "before listener");
                txtNote.Text = "Tick";
                Log.Debug(TAG, "after listener");
            };
        }

        protected override void OnResume()
        {
            base.OnResume();
            IntentFilter filter = new IntentFilter("com.companyname.testapp.MY_CUSTOM_ACTION");
            RegisterReceiver(catcher, filter);
        }

        protected override void OnPause()
        {
            base.OnPause();
            UnregisterReceiver(catcher);
        }

        private void BroadcastStarted()
        {
            // Broadcast message to the view

            Intent BroadcastIntent = new Intent();
            BroadcastIntent.SetAction("com.companyname.testapp.MY_CUSTOM_ACTION");
            //BroadcastIntent.AddCategory(Intent.CategoryDefault);
            Log.Debug(TAG, "Broadcast started");
            SendBroadcast(BroadcastIntent);
        }
    }



    [BroadcastReceiver]
    public class BroadcastMessageCatcher : BroadcastReceiver
    {
        private readonly string TAG = "MainActivity";

        public override void OnReceive(Context context, Intent intent)
        {
            if (intent.Action == "com.companyname.testapp.MY_CUSTOM_ACTION")
            {
                Log.Debug(TAG, "before event");
                TickEvent();
                Log.Debug(TAG, "after event");
            }
        }

        public delegate void TickEventHandler();
        public event TickEventHandler TickEvent;
    }
}
使用Android.App;
使用Android.Content;
使用Android.OS;
使用Android.Util;
使用Android.Widget;
命名空间TestApp
{
[活动(Label=“BroadcastUpdateFormExample”,MainLauncher=true)]
公共课活动:活动
{
整数计数=1;
文本视图txtNote;
广播消息捕捉器捕捉器;
静态只读字符串TAG=“FCMActivity”;
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
//从“主”布局资源设置视图
SetContentView(Resource.Layout.activity_main);
txtNote=findviewbyd(Resource.Id.textView1);
Button Button=FindViewById(Resource.Id.button1);
按钮。单击+=委派
{
button.Text=string.Format(“{0}点击!”,count++);
广播开始();
};
catcher=新广播消息catcher();
catcher.ticketvent+=委托
{
调试(标记为“侦听器之前”);
txtNote.Text=“勾选”;
调试(标记为“侦听器之后”);
};
}
受保护的覆盖void OnResume()
{
base.OnResume();
IntentFilter=newintentfilter(“com.companyname.testapp.MY_CUSTOM_ACTION”);
寄存器接收器(捕集器、过滤器);
}
受保护的覆盖void OnPause()
{
base.OnPause();
未注册接收器(捕获器);
}
私有void广播已启动()
{
//向视图广播消息
意向广播意向=新意向();
BroadcastIntent.SetAction(“com.companyname.testapp.MY_CUSTOM_ACTION”);
//BroadcastIntent.AddCategory(Intent.CategoryDefault);
调试(标记“广播已启动”);
发送广播(广播意图);
}