Reputation: 473
I want to set a schedule in my application. If my application is stopped by the application task killer, my application automatically runs after many seconds after it gets killed.
My application is like this :
public class AlarmServiceDemo extends Activity
{
/** Called when the activity is first created. */
private PendingIntent pendingIntent;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonStart = (Button)findViewById(R.id.startalarm);
Button buttonCancel = (Button)findViewById(R.id.cancelalarm);
buttonStart.setOnClickListener(new Button.OnClickListener()
{
// @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(AlarmServiceDemo.this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(AlarmServiceDemo.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(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Toast.makeText(AlarmServiceDemo.this, "Start Alarm", Toast.LENGTH_LONG).show();
}
});
buttonCancel.setOnClickListener(new Button.OnClickListener()
{
//@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
// Tell the user about what we did.
Toast.makeText(AlarmServiceDemo.this, "Cancel!", Toast.LENGTH_LONG).show();
}
});
}
}
And this is my service :
public class MyAlarmService extends Service
{
@Override
public void onCreate()
{
// TODO Auto-generated method stub
Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG).show();
}
@Override
public IBinder onBind(Intent intent)
{
// TODO Auto-generated method stub
Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG).show();
return null;
}
@Override
public void onDestroy()
{
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG).show();
}
@Override
public void onStart(Intent intent, int startId)
{
// TODO Auto-generated method stub
super.onStart(intent, startId);
Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();
}
@Override
public boolean onUnbind(Intent intent)
{
// TODO Auto-generated method stub
Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG).show();
return super.onUnbind(intent);
}
}
But my schedule doesn't work. How can I solved it?
Upvotes: 0
Views: 631
Reputation: 15477
you have to write exactly what you mean by starting application.If you want to start an Activity then you can write
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
Intent activityIntent = new Intent(context,
AlarmServiceDemo.class);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(activityIntent);
super.onStart(intent, startId);
}
Upvotes: 1