0

Trying to use a alarm manager to schedule 'my code' to run daily, i followed a tutorial and tried to adapt it for my code.

Two points:

1) am i using alarm manager in the correct way logically, where to put the code to be affected by alarm manager and the code that is not and just runs normally, and a little bit of information there.

2) Try and fix the log cat error, as you may have realized i am new to android development so am struggling to fix/find the error. - any insight into logcat finding the errors would also be useful.

Here is my 'MainACtivity.java':

public class MainActivity extends Activity {

    private PendingIntent pendingIntent;

    @Override   protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);         setContentView(R.layout.home);

                Intent myIntent = new Intent(MainActivity.this, Alarm.class);
                pendingIntent = PendingIntent.getService(MainActivity.this, 0, myIntent, 0);



        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);



        Calendar calendar = Calendar.getInstance();

        calendar.setTimeInMillis(System.currentTimeMillis());

        calendar.add(Calendar.SECOND, 10);

        alarmManager.set(Calendar.HOUR_OF_DAY, 0, pendingIntent);

        alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pendingIntent);         // time set up is unsuccessful in earlier versions

            }       @Override   public boolean onCreateOptionsMenu(android.view.Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        super.onCreateOptionsMenu(menu);        getMenuInflater().inflate(R.menu.main, menu);       return true;
            }



}

Here is my 'Alarm.java':

public class Alarm extends Service{



public static final String MyPREFERENCES = "crntPrefs";

    private Random rand = new Random();
    int[] ids = {R.string.word1, R.string.word2 //etc....};

    SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedpreferences.edit();



    public void onStart(Intent intent, int startId) {
        {
            //my code
    }

}

Logcat:

05-17 16:54:23.048: E/AndroidRuntime(1137): FATAL EXCEPTION: main
05-17 16:54:23.048: E/AndroidRuntime(1137): Process: com.knight.spreadtheword, PID: 1137
05-17 16:54:23.048: E/AndroidRuntime(1137): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.knight.spreadtheword/com.knight.spreadtheword.MainActivity}: java.lang.IllegalArgumentException: Invalid alarm type 11
05-17 16:54:23.048: E/AndroidRuntime(1137):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-17 16:54:23.048: E/AndroidRuntime(1137):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-17 16:54:23.048: E/AndroidRuntime(1137):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-17 16:54:23.048: E/AndroidRuntime(1137):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-17 16:54:23.048: E/AndroidRuntime(1137):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-17 16:54:23.048: E/AndroidRuntime(1137):     at android.os.Looper.loop(Looper.java:136)
05-17 16:54:23.048: E/AndroidRuntime(1137):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-17 16:54:23.048: E/AndroidRuntime(1137):     at java.lang.reflect.Method.invokeNative(Native Method)
05-17 16:54:23.048: E/AndroidRuntime(1137):     at java.lang.reflect.Method.invoke(Method.java:515)
05-17 16:54:23.048: E/AndroidRuntime(1137):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-17 16:54:23.048: E/AndroidRuntime(1137):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-17 16:54:23.048: E/AndroidRuntime(1137):     at dalvik.system.NativeStart.main(Native Method)
05-17 16:54:23.048: E/AndroidRuntime(1137): Caused by: java.lang.IllegalArgumentException: Invalid alarm type 11
05-17 16:54:23.048: E/AndroidRuntime(1137):     at android.os.Parcel.readException(Parcel.java:1469)
05-17 16:54:23.048: E/AndroidRuntime(1137):     at android.os.Parcel.readException(Parcel.java:1419)
05-17 16:54:23.048: E/AndroidRuntime(1137):     at android.app.IAlarmManager$Stub$Proxy.set(IAlarmManager.java:155)
05-17 16:54:23.048: E/AndroidRuntime(1137):     at android.app.AlarmManager.setImpl(AlarmManager.java:363)
05-17 16:54:23.048: E/AndroidRuntime(1137):     at android.app.AlarmManager.set(AlarmManager.java:191)
05-17 16:54:23.048: E/AndroidRuntime(1137):     at com.knight.spreadtheword.MainActivity.onCreate(MainActivity.java:59)
05-17 16:54:23.048: E/AndroidRuntime(1137):     at android.app.Activity.performCreate(Activity.java:5231)
05-17 16:54:23.048: E/AndroidRuntime(1137):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-17 16:54:23.048: E/AndroidRuntime(1137):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-17 16:54:23.048: E/AndroidRuntime(1137):     ... 11 more
user3165683
  • 347
  • 1
  • 9
  • 28
  • `Caused by: java.lang.IllegalArgumentException: Invalid alarm type 11` – nhaarman May 17 '14 at 21:14
  • @NiekHaarman pls expand, is there a way to know what line this is on and a fast way to find it in the code? – user3165683 May 17 '14 at 21:17
  • http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors – nhaarman May 17 '14 at 21:18
  • @NiekHaarman thank u, so this is the problem line ' alarmManager.set(Calendar.HOUR_OF_DAY, 0, pendingIntent);' with the error being 'Invalid alarm type 11' correct? so how do i solve this? – user3165683 May 17 '14 at 21:25

1 Answers1

0

According to the documentation of the AlarmManager class the type has to be one of ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP, RTC, or RTC_WAKEUP.

FabianTe
  • 516
  • 4
  • 22
  • changing .HOUR_OF_DAY to .RTC raises an error in line 59, was that what u were getting at? pls leave a solution, also should i change the calendar and alarm manager set to something like this [alarm manager tutorial](http://androidresearch.wordpress.com/2012/07/02/scheduling-an-application-to-start-later/) – user3165683 May 17 '14 at 22:03