• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Activity keeps reappearing

Geffa

Lurker
Hi

I'm an experienced developer although completely new to mobile development. I am trying to write an app which periodically sends an SMS message.

I do this by with my activity instatiating an AlarmManager when a button is clicked. The AlarmManager starts a service which sends the SMS.

All is fine except that when the SMS is transmitted the activity UI is brought to focus again regardless of what is running. I do not wish this to happen. Ultimately my UI will be a configuration of how the service will behave although I have not looked into the data interface between activity and service yet.

Any help much appreciated.

Thanks
Geoff
 
The code for the button to create the time is

alarm.SetAlarm(getApplicationContext());

The Alarm (based on BroadcastReceiver) is as follows

@Override
public void onReceive(Context context, Intent intent)
{
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG");
wl.acquire();

Intent myIntent = new Intent(context, MyService.class);
context.startService(myIntent);
wl.release();
}

public void SetAlarm(Context context)
{
//sendCurrentLocation(context);
//locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, Alarm.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 10, pi); // Millisec * Second * Minute
}


The MyService (based on Service) is

@Override
public void onStart(Intent intent, int startId) {
sendMessage();
}
 
Try putting a finish() on the context that is calling the service like this:

@Override
public void onReceive(Context context, Intent intent)
{
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG");
wl.acquire();

Intent myIntent = new Intent(context, MyService.class);
context.startService(myIntent);
context.finish();
wl.release();
}
 
Thanks for the advice but it gives a compilation error that

"method finish is undefined for type Context"
 
Back
Top Bottom