Reputation: 160
I'm developing an app which takes the system to silent mode at specific time of the day. I'm using alarm manager for this. I call the AddAlarm Activity after the registration activity. My problem is...The phone goes to silent mode only when I open the app but not at the scheduled time. I donno where I'm going wrong.
Here is my 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();
}
}
Here is my 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);
}
}
Here is my 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;
}
}
And my manifest file
<?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>
I'll be great if somebody can help me.
Upvotes: 0
Views: 74
Reputation: 20211
If you're opening the app after 7:20AM, then the alarm will be set for 7:20AM on the same day. The alarm will immediately fire in this case.
You can avoid this by evaluating if it is already past 7:20AM and setting the alarm for the next day:
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);
Upvotes: 1