广播接收器在设置警报管理器时在应用程序启动时触发

Broadcast reciever fired at the start of the app while setting alarm manager

我正在开发一个应用程序,它可以在一天中的特定时间让系统进入静默模式。我正在为此使用警报管理器。 我在注册activity后调用AddAlarmActivity。 我的问题是... phone 只有在我打开应用程序时才进入静音模式,但没有在预定时间进入。我不知道哪里错了。

这是我的 AddAlarms class

public class AddAlarms extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(this, ScheduledService.class);
        AlarmManager am1 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Calendar calendar1 = Calendar.getInstance();
        calendar1.set(Calendar.HOUR_OF_DAY, 7);
        calendar1.set(Calendar.MINUTE, 20);
        calendar1.set(Calendar.AM_PM, Calendar.AM);
        PendingIntent pi1 = PendingIntent.getBroadcast(this, 1421521, intent, 0);
        am1.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi1);

        startActivity(new Intent(this, NavActivity.class));
        finish();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }
}

这是我的 ScheduledService class

public class ScheduledService extends WakefulBroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
       Intent silService=new Intent(context,SilentService.class);
        context.startService(silService);
    }
}

这是我的 SilentService class

public class SilentService extends Service {
    private AudioManager myAudioManager;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        myAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        if (myAudioManager.getRingerMode() != AudioManager.RINGER_MODE_SILENT)
            myAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);


        return START_STICKY;
    }
}

还有我的清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="net.notifoid.mcestraddler" >


    <uses-permission android:name="com.android.alarm.permission.SET_ALARM" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        tools:replace="android:icon" >


        <activity
            android:name=".activities.RegisterActivity"
            android:label="@string/app_name"
            android:theme="@style/Theme.AppCompat.NoActionBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <activity
            android:name=".activities.NavActivity"
            android:label="@string/app_name"
            android:theme="@style/Theme.AppCompat.NoActionBar" />

        <activity
            android:name=".activities.AddAlarms"
            android:label="@string/app_name"
            android:theme="@style/Theme.AppCompat.NoActionBar" />

        <service
            android:name=".silentmode.SilentService"
            android:enabled="true"
            android:exported="false" />

        <receiver
            android:name=".silentmode.ScheduledService"
            android:enabled="true" >
        </receiver>

    </application>

</manifest>

如果有人能帮助我,我会很高兴。

如果您在 7:20AM 之后打开应用程序,则闹钟将设置为 7:20AM 当天。在这种情况下,警报会立即响起。

您可以通过评估它是否已经过去 7:20AM 并设置第二天的闹钟来避免这种情况:

Calendar calendar1 = Calendar.getInstance();
if(calendar1.get(Calendar.HOUR_OF_DAY) > 7 ||
        (calendar1.get(Calendar.HOUR_OF_DAY) == 7 && calendar1.get(Calendar.MINUTE) >= 20)){
    calendar1.add(Calendar.DATE, 1);
}

calendar1.set(Calendar.HOUR_OF_DAY, 7);
calendar1.set(Calendar.MINUTE, 20);