Restart my application after 3 seconds of being killed

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Restart my application after 3 seconds of being killed

Postby csengyee » Thu Sep 03, 2009 1:40 pm

Hi, I would like to restart my application after the user set his/her preferences (restart() method of my MyApplication class will be invoked). I am using an AlarmManager to start my application right after I kill it. I have no idea why my BroadcastReceiver is never called. Anyone can advise on how I can resolve this? Thanks.

public void restart() {
// Setup one-short alarm to restart my application in 3 seconds
AlarmManager alarmMgr=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent i=new Intent(context, RestartProcessor.class);
PendingIntent pi=PendingIntent.getBroadcast(context, 0, i, 0);
alarmMgr.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+3000, pi);

// Kill my application
ActivityManager actmgr=(ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
actmgr.restartPackage("com.MyApplication");
}


public class RestartProcessor extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
ActivityManager actmgr=(ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
try {
Intent i = new Intent(context, MyApplication.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService(i);
} catch (Exception e) {}
}
}
csengyee
Junior Developer
Junior Developer
 
Posts: 19
Joined: Mon Jul 13, 2009 3:07 pm

Top

Postby menion » Sat Feb 06, 2010 9:14 pm

Hi csengyee,
I see that this is an old topic, but I'm trying to solve similar problem. Seems your code is right except using context that you close on next line of code. So solution should be use another Context to start intent right? So any solution, cause I can't find anyone ... thanks
menion
Freshman
Freshman
 
Posts: 4
Joined: Wed Jan 13, 2010 12:36 pm

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 9 guests