因为没有收到来自IntentService的标题广播。 以下是我设置的方法:
[Service]
[IntentFilter(new[] { MyService.ToUploadCountNotification })]
public class MyService : IntentService
{
public const string ToUploadCountNotification = "MyService.ToUploadCountNotification";
public const string ToUploadCount = "MyService.ToUploadCount";
protected override void OnHandleIntent(Intent intent)
{
while (Count > 0)
{
var uploadCount = new Intent();
uploadCount.SetAction(ToUploadCountNotification);
uploadCount.PutExtra(ToUploadCount, localHarryys.Count);
Log.Debug("Sync service", "sending broadcast");
SendBroadcast(intent, null);
}
}
}
public class MyServiceBroadcastReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
//throw new NotImplementedException();
Toast.MakeText(context,
string.Format("Uploading...({0})", intent.Extras.GetInt(MyService.ToUploadCount)),
ToastLength.Short).Show();
}
}
在活动中我注册接收者:
protected override void OnResume()
{
base.OnResume();
RegisterReceiver(_receiver, new IntentFilter(MyHarryysSyncService.ToUploadCountNotification));
Log.Debug("Browse", "Receiver registered");
}
protected override void OnPause()
{
base.OnPause();
UnregisterReceiver(_receiver);
Log.Debug("Browse", "Receiver unregistered");
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
if (requestCode == 0)
{
StartService(new Intent(this, typeof(MyService)));
}
base.OnActivityResult(requestCode, resultCode, data);
}
在我的研究中,我能找到的只有
我已经测试了更多的东西。
行StartService(new Intent(this, typeof(MyService)))
之后
我放了SendBroadcast(new Intent(MyService.ToUploadCountNotification), null);
在我将注册移至OnCreate / OnDestroy之前,这种方法无效。从服务中不幸地广播仍未收到
我在 androidmanifest.xml 中注册了接收器,但是我想让它按活动动态注册,因为根据活动活动可能会有不同的行为。
所以我的问题是我在那里做错了什么。还有什么我可以检查。