Android studio Unity-未调用Android广播接收器

Android studio Unity-未调用Android广播接收器,android-studio,unity3d,android-alarms,android-unity-plugin,Android Studio,Unity3d,Android Alarms,Android Unity Plugin,我正在使用Unity为一个项目设置警报。(通常不会使用统一,但出于不同的原因,这是一个要求)。 我写了一个类似这样的报警,这个方法从Unity调用,参数为5: public void SetAlarm (float time) { Intent intent = new Intent(context, AlarmClockPlugin.class); intent.setAction("ALARM_INTENT"); PendingInte

我正在使用Unity为一个项目设置警报。(通常不会使用统一,但出于不同的原因,这是一个要求)。 我写了一个类似这样的报警,这个方法从Unity调用,参数为5:

public void SetAlarm (float time)
    {
        Intent intent = new Intent(context, AlarmClockPlugin.class);
        intent.setAction("ALARM_INTENT");
        PendingIntent pending = PendingIntent.getBroadcast(context,0,intent,0);
        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (long) (1000 * time), pending);
}
此报警类还继承自BroadcastReceiver并重写OnReceive方法,如下所示:

@Override
    public void onReceive(Context context, Intent intent) {
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
        wl.acquire();

        // Write toast text for now
        Toast.makeText(context,"Broadcast received",Toast.LENGTH_LONG).show();

        wl.release();
    }
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tombuild.myapp">
<uses-permission android:name="android.permission.WAKE_LOCK"/>

<application
    android:allowBackup="true"
    android:label="@string/app_name"
    android:supportsRtl="true">
    <receiver android:process =":remote" android:name="com.tombuild.myapp.AlarmClockPlugin">
        <intent-filter>
            <action android:name="ALARM_INTENT"/>
        </intent-filter>
        </receiver>
</application>

</manifest>
我在清单中注册广播接收器,如下所示:

@Override
    public void onReceive(Context context, Intent intent) {
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
        wl.acquire();

        // Write toast text for now
        Toast.makeText(context,"Broadcast received",Toast.LENGTH_LONG).show();

        wl.release();
    }
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tombuild.myapp">
<uses-permission android:name="android.permission.WAKE_LOCK"/>

<application
    android:allowBackup="true"
    android:label="@string/app_name"
    android:supportsRtl="true">
    <receiver android:process =":remote" android:name="com.tombuild.myapp.AlarmClockPlugin">
        <intent-filter>
            <action android:name="ALARM_INTENT"/>
        </intent-filter>
        </receiver>
</application>

</manifest>


但不幸的是,onReceive没有被调用——在5秒的持续时间之后根本没有弹出toast通知,这是为什么

我不熟悉Unity,但是Unity和Android Studio与您发布的代码有明显的区别,例如清单中没有活动。为什么不在setAlarm方法中尝试一个更简单的例子呢

    ...
    Intent intent = new Intent();
    intent.setAction("com.tombuild.myapp.ALARM_INTENT");
    sendBroadcast(intent);
    ...
在onReceive方法中放一个祝酒词

在TutorialPoint上有一个关于广播接收器的非常简单的教程,这让我开始学习