views:

205

answers:

0

In order to learn how to use the AlarmManager I created an activity that consists of a single button. When the button is pressed the activity creates an Intent of itself and loads it into the AlarmManager 3 seconds in the future before finishing.

So activity opens, user pushes button, activity closes, 3 seconds later activity opens, repeat.

Problem is instead of opening an activity 3 seconds later I get an error: "The application [myappname] has stopped unexpectedly. Please try again." Except when I call a Toast instead, that worked.

Here's the code for the button:

thanksButton.setOnClickListener(new View.OnClickListener() {
               public void onClick(View view) {
               setResult(RESULT_OK);
               Intent intent = new Intent(myappname.this,
myappnameBroadcastReceiver.class);
               PendingIntent appIntent = PendingIntent.getBroadcast
(myappname.this, 0, intent, 0);
               Calendar calendar = Calendar.getInstance();
               calendar.setTimeInMillis(System.currentTimeMillis());
               calendar.add(Calendar.SECOND, 3);
               AlarmManager am = (AlarmManager)getSystemService
(ALARM_SERVICE);
               am.set(AlarmManager.RTC, calendar.getTimeInMillis(),
appIntent);

               finish();
               }

       });

Here's the broadcast receiver:

package com.myappname;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class myappnameBroadcastReceiver extends BroadcastReceiver {
       @Override
       public void onReceive(Context context, Intent intent) {
                       context.startActivity(new Intent(context, myappname.class));
       }
}

and here's the lines from the manifest.xml

       <receiver
               android:name=".myappnameBroadcastReceiver"
               android:process=":remote">
       </receiver>