I've poured through a dozen tutorials and forum answers about this problem, but still haven't been able to get some working code together. I'll try to keep the question straightforward:
How do you use AlarmManager (in the Android API) to start an Activity at a given time? Any solution to this problem will do.
My latest attempt to achieve this is below.
(Imports omitted. I expect MyActivity to start 3 seconds after the program is opened, which it doesn't. There are no error messages to speak of.)
public class AndroidTest2Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Context context = this;//.getApplicationContext();
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); // CORRECT
Intent intent = new Intent(context, myReceiver.class); // CORRECT
PendingIntent pending = PendingIntent.getBroadcast( context, 0, intent, 0 ); // CORRECT
manager.set( AlarmManager.RTC, System.currentTimeMillis() + 3000, pending ); // CORRECT
setContentView(R.layout.main);
}
}
public class myReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Intent i=new Intent(context, myActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
public class myActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("", "Elusive success");
setContentView(R.layout.main);
}
}
Any advice would be appreciated.
Please note: I've got myReceiver in the manifest already
Supply a
PendingIntent
to theset()
call that identifies the activity to start up. Or, do what you're doing, which should work just fine.This sample project is a bit elaborate, because it's 19 tutorials deep into one of my books, but if you look at classes like
EditPreferences
,OnBootReceiver
, andOnAlarmReceiver
, you will see the same basic recipe that you're using above. In this case, I could have just used agetActivity()
PendingIntent
, but the tutorial after this one gives the user a choice of launching an activity or displaying aNotification
, so aBroadcastReceiver
makes more sense.Look for warnings in addition to errors in LogCat. Most likely, your receiver or activity is not in your manifest.
Note that popping up an activity out of the middle of nowhere is generally not a good idea. Quoting myself from the book in question:
In my experience you can achieve this without broadcast receiver, just use
PendingIntent.getActivity()
instead ofgetbroadcast()
I've tested this code on android O but I'm not sure about other android versions please inform me if this doesn't work on any other android version.
you are not sending any broadcast for the receiver to receiver and further more it lokks like u want a splash screen or something like that for that purpose u can start a new thread wait for some sec then start ur activity in that and for that time period u can do what ever u want on the UI thread ...
add this in your android mainifest file and it will hopefully work
According to Java convention class name begin with Capital letter.So change your
Then add your receiver in the manifest file like the below.
In case someone else stumbles upon this - here's some working code (Tested on 2.3.3 emulator):
AndroidManifest.xml:
Issues with BenLambell's code :
<receiver android:name="package.name.MainActivity$AlarmReceiver" ></receiver>
<receiver android:name="package.name.AlarmReceiver" ></receiver>
If your intention is to display a dialog in the receiver's onReceive (like me): that's not allowed - only activities can start dialogs. This can be achieved with a dialog activity.
You can directly call an activity with the AlarmManager: