我正在开发一个应用程序,它可以在一天中的特定时间将系统置于静音模式。我为此使用了报警管理器。 我在注册活动后调用AddAlarm Activity。 我的问题是......只有当我打开应用程序时,手机才会进入静音模式,但不是在预定时间。我不知道我哪里出错了。
这是我的AddAlarms类
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类
public class ScheduledService extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent silService=new Intent(context,SilentService.class);
context.startService(silService);
}
}
这是我的SilentService类
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>
如果有人可以帮助我,我会很棒。
答案 0 :(得分:1)
如果您在上午7:20之后打开应用程序,则闹钟将在同一天的上午7:20设置。在这种情况下,警报会立即触发。
您可以通过评估它是否已经过了上午7:20并设置第二天的闹钟来避免这种情况:
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);