【问题标题】:AlarmManager not cancelingAlarmManager 未取消
【发布时间】:2020-01-15 06:02:08
【问题描述】:

您好,我是 android 新手,我正在尝试制作一个在设备充电时检查电池状态的应用。如果电池电量超过设置中的定义,它将显示通知并响铃。

我现在只是在插入设备后每个30 second 之后测试它和showing notification。根据我的逻辑alarm manger将在设备插入时设置,当设备为plugged out时将设置cancel pending of alarm manager

但它不起作用,通知也显示 after disconnecting device 。我不知道为什么会这样。我试图取消挂起,但它不起作用。

请帮助我这里发生了什么。

自定义接收器

public class CustomReceiver extends BroadcastReceiver {

    AlarmManager alarmManager;
    PendingIntent p;

    @Override
    public void onReceive(Context context, Intent intent) {
        String intentAction = intent.getAction();
        if (intentAction != null) {
            if (intentAction.equals(Intent.ACTION_POWER_CONNECTED)) {
                toast("Power Connected", context);
                setBatteryAlarm(context);
            }
            if (intentAction.equals(Intent.ACTION_POWER_DISCONNECTED)) {
                toast("Power Disconneted", context);
                cancelAlarm();
            }
        }
    }


    public void toast(String msg, Context context) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }

    private void setBatteryAlarm(Context context) {
        Intent notifyIntent = new Intent(context, BatteryCheckingReciever.class);
        p = PendingIntent.getBroadcast(context, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        long repeatInterval = 30000;
        long triggerTime = SystemClock.elapsedRealtime() + repeatInterval;

        alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerTime, repeatInterval, p);
    }

    private void cancelAlarm() {
        Log.e("kiran", "canceling alarm");
        if (alarmManager != null) {
            alarmManager.cancel(p);
            alarmManager = null;
        }
    }
}

用于通知的广播接收器

public class BatteryCheckingReciever extends BroadcastReceiver {

    NotificationManager notificationManager;
    int NOTIFICATION_ID = 0;
    String PRIMARY_CHANNEL = "battery_is_full";

    @Override
    public void onReceive(Context context, Intent intent) {

        showNotificaiton(context);
    }

    private void showNotificaiton(Context context) {
        notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, PRIMARY_CHANNEL)
                .setSmallIcon(R.drawable.ic_battery_full)
                .setContentTitle(context.getString(R.string.battery_full))
                .setContentText(context.getString(R.string.battery_full_text))
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setAutoCancel(true)
                .setDefaults(NotificationCompat.DEFAULT_ALL);

        notificationManager.notify(NOTIFICATION_ID, builder.build());
    }
}

清单文件

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <receiver
            android:name=".BatteryCheckingReciever"
            android:enabled="true"
            android:exported="true"></receiver>
        <receiver
            android:name=".CustomReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
                <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
            </intent-filter>
        </receiver>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.action.VIEW" />

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

抱歉英语不好。

【问题讨论】:

    标签: android broadcastreceiver alarmmanager custom-notification


    【解决方案1】:

    这是因为您对警报管理器和待定意图更改的参考。很可能您的警报管理器变为空,但是因为您正在进行空检查,所以您没有错误。您需要再次引用相同的对象

    像这样更改您的代码:

      private void cancelAlarm() {
    
     alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent notifyIntent = new Intent(context, BatteryCheckingReciever.class);
     p = PendingIntent.getBroadcast(context, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    
    
            if (alarmManager != null) {
           Log.e("kiran", "canceling alarm");
                alarmManager.cancel(p);
                alarmManager = null;
            }
        }
    

    https://stackoverflow.com/a/4350149/11982611

    【讨论】:

    • 当我在试验时,我想做一些不同的事情,这意味着我在服务中设置警报,在 ACTION_POWER_CONNECTED 开始,广播接收器在 ACTION_POWER_DISCONNECTED 停止该服务。我还在服务开始时提供正在进行的通知,并取消该服务和电池电量检查到其他服务,但问题是当手机屏幕打开时警报工作正常,但在睡眠或充电时警报不起作用。我正在使用 setInExactRepeating,而且我的设备版本是 nogut。
    • 使用 setExactAndAllowWhileIdle 这个
    • 感谢您的回复,但它没有按预期工作,我试图制作没有警报管理器的应用程序。概念是在电源连接广播上启动服务,并在该服务寄存器中广播 BATTERY_CHANGE 。因此,每当我们收到电池更改广播时,我们都可以检查电池的状态,它工作得非常好,根本不需要警报管理器。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多