警报被应用程序杀死

Alarm is getting killed with app

警报不起作用的程度越来越不真实了。我有 2 个问题 Android 杀死我的应用程序,因此一切都停止了,即使我读到的所有内容都说它应该工作。

当我对我的应用程序进行绿色化时,我的 AlarmManager 项目消失了,对我来说,绿色化与让 phone 坐在那里并让 Android 杀死它是一样的。无论哪种方式,我都应该在等待触发我的 PwendingIntent 的提示中设置一个警报。不然闹钟设置有什么用RTC_WAKEUP

我的清单

<receiver
    android:name=".ContactAlarmReceiver"
    android:enabled="false"
    android:process=":alarmremote">
    <intent-filter>
        <action android:name="com.example.johnbravado.zionwork.CONTACTALARM" />
        <action android:name="com.example.johnbravado.zionwork.NOTIFLTBTN" />
        <action android:name="com.example.johnbravado.zionwork.NOTIFRTBTN" />
    </intent-filter>
</receiver>

我的接收器

@Override
public void onReceive(Context context, Intent intent) {
    // TODO: This method is called when the BroadcastReceiver is receiving
    // an Intent broadcast.
    //throw new UnsupportedOperationException("Not yet implemented");
    //Toast.makeText(context, "Received Alarm", Toast.LENGTH_SHORT).show();
    Log.d("johnbravadoCAR ","received");
    PowerManager powerManager = (PowerManager) context.getSystemService(POWER_SERVICE);
    PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
            "com.example.johnbravado.zionwork");
    wakeLock.acquire();

    ComponentName component = new ComponentName(context, ContactAlarmIntentService.class);
    context.getPackageManager()
            .setComponentEnabledSetting(component,
                    PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                    PackageManager.DONT_KILL_APP);

    Intent alarmIntent = new Intent(context, ContactAlarmIntentService.class);
    alarmIntent.putExtras(intent.getExtras());
    alarmIntent.setAction(intent.getAction());
    context.startService(alarmIntent);
    wakeLock.release();
}

我知道清单中有 android:enabled="false"。我在设置警报之前通过调用 PackageManager 来反击。见下文

private void setAlarm() {
    //ContactAlarmReceiver contactAlarmReceiver = new ContactAlarmReceiver();
    //contactAlarmReceiver.setAlarm(getApplicationContext(), dateInMillis, phonenumberET.getText().toString(), firstnameET.getText().toString());

    ComponentName component = new ComponentName(this, ContactAlarmReceiver.class);
    getPackageManager()
            .setComponentEnabledSetting(component,
                    PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                    PackageManager.DONT_KILL_APP);

    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, 123456, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    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);
    }
}

一旦我将闹钟设置的时间超过 30 分钟,或者如果我将闹钟设置为 3 分钟后并对我的应用程序进行 Greenify,我在 [=36] 中看不到 Log.d 打印出来=] 查看器。 AlarmManager,据我所知,应该触发我的 BroadcastReceiver 并开始工作。

如果我能做到这一点,我会写一个详细的解释,这样其他人就不会经历同样的事情。如果这应该有效,是否有某些设置可能会终止与我需要取消选中的应用程序的关联?

更新:

这段代码很可靠。我在使用辅助应用程序 Greenify 时遇到问题,它在我的直接交互之外以某种方式被打开。当屏幕关闭时,那个应用程序正在杀死我的应用程序。在告诉 Greenify 不要触摸我的应用程序之后,此代码会根据需要执行。从好的方面来说,我学到了很多关于 BroadcastReceiver、AlarmManager 和服务实现的知识,因为我尝试了 10 种不同的方法来解决我无法用代码解决的问题。

Greenify 正在终止我的应用程序。请参阅 OP

中的更新