android schedule task to execute at specific time daily-ThrowExceptions

Exception or error:

I am wanting to change my ringtone volume at specific time each day. I used a calendar to specify the time, and I am attempting to use alarmManager to execute it. Here is what I have. (I am a noobie go easy on me)


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import java.util.Calendar;

public class MainActivity extends ActionBarActivity {

    private AudioManager audio;
    private PendingIntent pendingIntentam;
    private PendingIntent pentdingIntentpm;
    protected void onCreate(Bundle savedInstanceState) {

        TextView textViewTime = (TextView) findViewById (;
        audio = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

        //ringer volume for the am.
        Calendar calendaram = Calendar.getInstance();

        calendaram.set(Calendar.HOUR_OF_DAY, 21);
        calendaram.set(Calendar.MINUTE, 32);
        calendaram.set(Calendar.SECOND, 0);

        Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
        pendingIntentam = PendingIntent.getBroadcast(MainActivity.this, 0,myIntent,0);
        AlarmManager alarmManageram = (AlarmManager)getSystemService(ALARM_SERVICE);
        alarmManageram.set(AlarmManager.RTC, calendaram.getTimeInMillis(), pendingIntentam);


    public class MyReceiver extends BroadcastReceiver {

        public void onReceive(Context context, Intent intent) {
                Intent scheduledIntent = new Intent(context, myscheduleactivity.class);


    public class myscheduleactivity extends Activity {

        protected void onCreate(Bundle savedInstanceState) {

    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(, menu);
        return true;

    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == {
            return true;
        return super.onOptionsItemSelected(item);

How to solve:

To fire alarm everyday, at 21:32

private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

// Set the alarm to start at 21:32 PM
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 21);
calendar.set(Calendar.MINUTE, 32);

// setRepeating() lets you specify a precise custom interval--in this case,
// 1 day
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
        AlarmManager.INTERVAL_DAY, alarmIntent);

In this case, AlarmReceiver is the Broadcast Receiver and it already has a context, so you can directly set the ringer mode to silent from the Broadcast Receiver without starting an activity.

    public void onReceive(Context context, Intent intent) {
       AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);   

Please be sure to add these permissions :

<uses-permission android:name="android.permission.WRITE_SETTINGS" ></uses-permission>
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" ></uses-permission>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" ></uses-permission>


If Shivam Verma’s answer does not work for you, make sure you add the receiver to your manifest:

<receiver android:name=".receivers.MyReceiver" />

Leave a Reply

Your email address will not be published. Required fields are marked *