首页 > 解决方案 > AlarmManager Dont Work 无法放置清单部分

问题描述

普利斯。我正在制作一个 AlarmManager 用于在 1 分钟内(在后台)重复该动作,但它不起作用,任何人都可以帮助我吗?我的主要活动

      int repeatTime = 60;  //Repeat alarm time in seconds
        AlarmManager processTimer = (AlarmManager)getSystemService(ALARM_SERVICE);
        Intent intent = new Intent(this, onBackround.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,  intent, PendingIntent.FLAG_UPDATE_CURRENT);
//Repeat alarm every second
        processTimer.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),repeatTime*1000, pendingIntent);`

我的广播收到:

     package com.example.hatzalahrescatista;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class onBackround extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        //Do something every 1 MINUTE
    }
}`

清单(我不能为此警报管理器提供许可)示例:()使我出现类似“未解决的课程......”之类的错误

```
 <application

    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"

    android:usesCleartextTraffic="true"
    android:theme="@style/AppTheme.NoActionBar">
    <activity android:name=".ActividadPrincipal">
        <intent-filter>
            <action android:name="com.example.hatzalahrescatista_TARGET_NOTIFICATION"></action>
        </intent-filter>
    </activity>
    <activity android:name=".ActividadLogin">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service
        android:name="MyFirebaseMessagingService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>


</application>

```

标签: javaandroidalarmmanager

解决方案


根据Android 文档,AlarmManager 是不准确的

这些警报会在空闲时显着影响设备的电源使用(从而导致调度它们的应用程序严重指责电池),因此应谨慎使用它们。为了减少滥用,对特定应用程序的警报响起频率有限制。在正常的系统操作下,它不会超过大约每分钟发送这些警报(此时每个此类挂起的警报都会被发送);在低功耗空闲模式下,此持续时间可能会更长,例如 15 分钟。

因此,如果您希望您的工作每分钟只执行一次,我建议使用Timer


推荐阅读