gpt4 book ai didi

android - Monodroid Intent Service 的 BroadcastReceiver 没有触发 OnReceive

转载 作者:太空宇宙 更新时间:2023-11-03 13:34:48 25 4
gpt4 key购买 nike

我一直在尝试为 monodroid 运行玩具 Intent 服务 essentially going off of this source material我一直遇到一些问题,我已经将其归结为广播接收器。最让我怀疑的是 ACTION_RESP 字符串。如果没有别的,我当然可以忍受,也许可以确认这一点,也许是设置接收者可以看到的 Action 的正确方法。
所以,这就是我到目前为止所拥有的...

我的意向服务

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

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

namespace Mono_For_Android_Intent_Service
{
[Service]
public class TestIntentService : IntentService
{
public static String PARAM_IN_MSG = "imsg";
public static String PARAM_OUT_MSG = "omsg";

#region Constructor
public TestIntentService()
: base("TestIntentService")
{
}
#endregion

protected override void OnHandleIntent(Intent intent)
{
string msg = intent.GetStringExtra(PARAM_IN_MSG);
//Do Stuff
string result = msg + " " + DateTime.Now.ToString();
Intent BroadcastIntent = new Intent(this,typeof(Mono_For_Android_Intent_Service.MainActivity.MyBroadcastReceiver));
BroadcastIntent.SetAction(Mono_For_Android_Intent_Service.MainActivity.MyBroadcastReceiver.ACTION_RESP);
BroadcastIntent.AddCategory(Intent.CategoryDefault);
BroadcastIntent.PutExtra(PARAM_OUT_MSG,result);
SendBroadcast(BroadcastIntent);
}
}
}

主要 Activity 和广播接收器

using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace Mono_For_Android_Intent_Service
{
[Activity(Label = "Mono_For_Android_Intent_Service", MainLauncher = true, Icon = "@drawable/icon")]
public partial class MainActivity : Activity
{
private MyBroadcastReceiver Receiver;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);

// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);

IntentFilter filter = new IntentFilter(MyBroadcastReceiver.ACTION_RESP);
filter.AddCategory(Intent.CategoryDefault);
Receiver = new MyBroadcastReceiver();
RegisterReceiver(Receiver, filter);

var button = FindViewById<Button>(Resource.Id.start);
button.Click += (s,e)=>
{
Intent msgIntent= new Intent(this,typeof(TestIntentService));
msgIntent.PutExtra(TestIntentService.PARAM_IN_MSG,"Me message, arh");
Toast.MakeText(this, "Starting intent service!", ToastLength.Short).Show();
StartService(msgIntent);

};

TextView txtView = FindViewById<TextView>(Resource.Id.status);
}

public class MyBroadcastReceiver : BroadcastReceiver
{
public static readonly string ACTION_RESP = "Mono_For_Android_Intent_Service.Intent.Action.MESSAGE_PROCESSED";
public override void OnReceive(Context context, Intent intent)
{
Toast.MakeText(context, "Firing On Receive!", ToastLength.Short).Show();
var activity = (MainActivity)context;
TextView txtView = activity.FindViewById<TextView>(Resource.Id.status);
txtView.Text = intent.GetStringExtra(TestIntentService.PARAM_OUT_MSG).ToString();
Toast.MakeText(context, "On Receive complete!", ToastLength.Short).Show();
}
}
}
}
  1. 我做错了吗?我很接近吗?我想认为我非常接近,但是无论如何,如果我在 timbucktoo,我想知道......
  2. 如果是这样,发送和接收该广播的正确方法是什么?

最佳答案

如果你想让你的广播接收器注册到系统,你需要添加[BroadcastReceiver]属性。

示例:

https://github.com/xamarin/monodroid-samples/blob/master/ApiDemo/App/OneShotAlarm.cs

关于android - Monodroid Intent Service 的 BroadcastReceiver 没有触发 OnReceive,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8686016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com