首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >持久告警

持久告警
EN

Stack Overflow用户
提问于 2017-01-14 12:17:40
回答 0查看 623关注 0票数 1

我正在试着设置一个警报器,它会一直开着,因为我想让它在3天后关闭,我知道Android会在那之前杀死我的应用程序。

在我的活动

代码语言:javascript
复制
private void setAlarm() {
    //ContactAlarmReceiver contactAlarmReceiver = new ContactAlarmReceiver();
    //contactAlarmReceiver.setAlarm(getApplicationContext(), dateInMillis, phonenumberET.getText().toString(), firstnameET.getText().toString());
    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent alarmIntent = new Intent(MyConstants.BROADCAST_ACTION_CONTACT_ALARM);
    alarmIntent.putExtra("phone", phonenumberET.getText().toString());
    alarmIntent.putExtra("name", firstnameET.getText().toString());

    PendingIntent pi = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
    //am.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeInMills, pi);
    if (Build.VERSION.SDK_INT >= 23) {
        am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, dateInMillis, pi);
    } else if (Build.VERSION.SDK_INT >= 19) {
        am.setExact(AlarmManager.RTC_WAKEUP, dateInMillis, pi);
    } else {
        am.set(AlarmManager.RTC_WAKEUP, dateInMillis, pi);
    }
    ComponentName component = new ComponentName(this, ContactAlarmReceiver.class);
    getPackageManager()
        .setComponentEnabledSetting(component,
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);
}

清单:

代码语言:javascript
复制
<receiver
    android:name=".ContactAlarmReceiver"
    android:enabled="false">
    <intent-filter>
        <action android:name="com.example.johnbravado.CONTACTALARM" />
        <action android:name="com.example.johnbravado.NOTIFLTBTN" />
        <action android:name="com.example.johnbravado.NOTIFRTBTN" />
    </intent-filter>
</receiver>

ContactAlarmReceiver:

代码语言:javascript
复制
@Override
@Override
public void onReceive(Context context, Intent intent) {
    PowerManager powerManager = (PowerManager) context.getSystemService(POWER_SERVICE);
    PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
            "com.example.johnbravado.zionwork");
    wakeLock.acquire();

    final NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Bundle extras = intent.getExtras();
    ComponentName receiver = new ComponentName(context, ContactAlarmReceiver.class);
    PackageManager pm = context.getPackageManager();

    if (intent.getAction().equals(MyConstants.BROADCAST_ACTION_CONTACT_ALARM)) {
        int count = extras.size();
        String num = extras.getString("phone");
        String name = extras.getString("name");
        sendContactNotification(context, MyConstants.BIG_NOTIFICATION_ID, num, name);
    }

    if (intent.getAction().equals(MyConstants.BROADCAST_ACTION_NOTIF_LTBTN)) {
        sendSMS(context, extras.getString("phone"), extras.getString("msg"));
        mNotificationManager.cancel(MyConstants.BIG_NOTIFICATION_ID);
        pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);
    }

    if (intent.getAction().equals(MyConstants.BROADCAST_ACTION_NOTIF_RTBTN)) {
        initiatePhone(context, intent.getExtras().getString("phone"));
        mNotificationManager.cancel(MyConstants.BIG_NOTIFICATION_ID);
        pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);
    }
    wakeLock.release();
}

在很长一段时间后,我从来没有收到过通知。例如,我将闹钟设置在晚上11点到凌晨3点,所以当我醒来时我应该会看到通知。但是没有显示任何通知。

EN

回答

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

https://stackoverflow.com/questions/41646749

复制
相关文章

相似问题

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