我正在使用 Unity 为项目设置警报。(通常不会使用 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>
但不幸的是,没有调用 onReceive - 5 秒后根本没有弹出 toast 通知,这是为什么呢?