首页 > 解决方案 > Xamarin Android BootReceiver 检查包

问题描述

当我自己的包安装/更新时,我尝试进行 BootReceiver 调用,但我似乎无法找到如何检查调用的原因是因为代码所在的包,还是另一个已更新的应用程序。这样的东西是否已经存在,如果存在,如何使用它?

这是代码:

[BroadcastReceiver(Enabled = true, Exported = true, DirectBootAware = true)]
[IntentFilter(new string[] { Intent.ActionBootCompleted, Intent.ActionLockedBootCompleted, Intent.ActionPackageReplaced, "android.intent.action.QUICKBOOT_POWERON", "com.htc.intent.action.QUICKBOOT_POWERON" })]
public class BootReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        Intent timedIntent = new Intent(context, typeof(ITM.NotificationService));
        //add missed traffic before program started with boot
        if(intent.Action.Equals(Intent.ActionBootCompleted) || intent.Action.Equals(Intent.ActionLockedBootCompleted))
        {
            timedIntent.PutExtra("aDownBootup", TrafficStats.TotalRxBytes);
            timedIntent.PutExtra("aUpBootup", TrafficStats.TotalTxBytes);
        }
        if (ITM.NotificationService.isStarted == false)
        {
            Application.Context.StartService(timedIntent);
        }
    }
}

标签: c#androidxamarinfilter

解决方案


感谢@android 开发者的回答

您可以使用ACTION_MY_PACKAGE_REPLACED操作:

您的应用程序的新版本已安装在现有版本之上。这只会发送到被替换的应用程序。它不包含任何附加数据;要接收它,只需为此操作使用意图过滤器。


推荐阅读