The iCoder
The iCoder

Reputation: 1444

On device reboot alarm is not going off at specific time

I am novice to android. I am developing alarm application in android. I have done following code which will work when device is on but on device reboot it will not work for me. I stored that alarm in shared preferences and retrieved from it. When device reboot I reschedule alarm from OnBootReceiver . I have already mentioned permissions to Android-manifest.For testing purpose I have taking hard-coded values. Please check following code and help me I researching on it from one and half day. Anyone has idea. Thanks.

public class FirstActivity extends Activity implements OnClickListener{

  int mHour = 14;
  int mMinute = 48;
  static String prefkey="SHARED_KEY";

public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);
   SharedPreferences preferences  =getSharedPreferences(prefkey,Context.MODE_WORLD_READABLE);


        SharedPreferences.Editor editor = preferences.edit();
        editor.putInt("min",mMinute);
        editor.putInt("hour",mHour);
        editor.commit();
   }
}

public class OnBootReciever extends BroadcastReceiver {

    int sethour,setmin;
@Override
public void onReceive(Context context, Intent intent) {

    Toast.makeText(context, "booting....", Toast.LENGTH_LONG).show();
    SharedPreferences preferences=context.getSharedPreferences(FirstActivity.prefkey,Context.MODE_PRIVATE);
    sethour=preferences.getInt("hour",14);
    setmin=preferences.getInt("min",48);
    Calendar cal=Calendar.getInstance();
    cal.add(Calendar.MINUTE,setmin);
    cal.add(Calendar.HOUR_OF_DAY,sethour);
    cal.add(Calendar.SECOND,0);


    AlarmManager mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent i=new Intent(context,RepeatingAlarm.class);

    PendingIntent sender1 = PendingIntent.getBroadcast(context,0, i,PendingIntent.FLAG_UPDATE_CURRENT);

    mgr.setRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),AlarmManager.INTERVAL_FIFTEEN_MINUTES,sender1);

}

}

public class RepeatingAlarm extends BroadcastReceiver {

   static MediaPlayer mMediaPlayer ;

@Override
public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "Trigger the alarm", Toast.LENGTH_LONG).show();
    mMediaPlayer = new MediaPlayer();
   mMediaPlayer.create(getcontext,R.raw.warm).start();
   }

}

In AndroidManifest.xml-->

 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
 <receiver android:name="com.vidushi.alarmsystem.RepeatingAlarm"></receiver>
        <receiver android:name=".OnBootReciever" android:process=":remote">
            <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <category android:name="android.intent.category.HOME" />
            </intent-filter>
         </receiver>

Upvotes: 0

Views: 675

Answers (2)

DaveSav
DaveSav

Reputation: 1374

Is your app installed on the sd card? If it is, it wont receive 'boot completed' notifications.

See App Install Location

Upvotes: 0

Mikita Belahlazau
Mikita Belahlazau

Reputation: 15434

May be when android calls your OnBootReceiver class, it passes it's own context, not context from your application. So it can't find shared preferences because android doesn't have them. Try to use your own context instead of one you get in onReceive method. You can create Application class with context like this and initialize it on application start:

import android.content.Context;

public class Application extends android.app.Application {

    private static Context context;

    public void onCreate(){
        context=getApplicationContext();
    }

    public static Context getContext() {
        return context;
    }

}

You also need to add following attribute to tag in AndroidManifest.xml:

android:name=".Application"

Link

And then use Application to get shared preferences:

Context context = Application.getContext();
SharedPreferences preferences=context.getSharedPreferences(FirstActivity.prefkey,Context.MODE_PRIVATE);

Upvotes: 1

Related Questions