首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AlarmManager未取消

AlarmManager未取消
EN

Stack Overflow用户
提问于 2020-01-15 14:02:09
回答 1查看 120关注 0票数 0

你好,我是android的新手,我正在尝试制作一个应用程序,当设备充电时,将检查电池状态。如果电池电量超过setting than中的定义,它将显示一条通知,还会响铃。

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

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

请告诉我这里发生了什么事。

CustomReceiver

代码语言:javascript
复制
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;
        }
    }
}

通知的BroadCast接收器

代码语言:javascript
复制
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());
    }
}

清单文件

代码语言:javascript
复制
<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>

对不起,我的英语不好。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-15 17:15:55

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

像这样修改你的代码:

代码语言:javascript
复制
  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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59745861

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档