Answer the question
In order to leave comments, you need to log in
What is the exit sequence of an application in Android?
Created an application with a broadcast service that is activated by the AlarmManager for a short moment, does some work, and then the service "dies" until the next cycle. The application has a glitch in the main activity, which does not interfere with the operation of the application, but is visible when the logger is connected. And I would like to fix it.
The application uses three main objects in the chain:
Answer the question
In order to leave comments, you need to log in
I didn’t quite understand who generates and who receives broadcasts from you. AlarmManager, Service, Activity?
When a certain time comes, the AlarmManager executes the PendingIntent passed to it, which, as I understand it, starts the service. The service does something, sends a broadcast, and exits. The receiver defined inside the activity is subscribed to this broadcast, and therefore, if the activity is launched, it should show something.
If this is how it works, then all you have to do is describe the appropriate PendingIntent and pass it to the AlarmManager. for example like this:
public class ScheduleAlarmActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
setAlarm(System.currentTimeMills() + 1000);
}
public void setAlarm(long time) {
Intent intent=new Intent(this, BroadcastService.class);
intent.putExtra("INTENT_DATA", "some intent data");
PendingIntent pi= PendingIntent.getService(context,0, intent,0);
AlarmManager am=(AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, time, pi);
}
}
public class BroadcastService extends Service {
// ....
public int onStartCommand(Intent intent, int flags, int startId) {
String intnetData = intent.getString("INTENT_DATA", "");
// делаем свою работу
Intent broadcast = new Intent("SOME_ACTIVITY_ACTION");
LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
stopSelf();
return START_NOT_STICKY;
}
}
public class ActionActivity extends AppCompatActivity {
// ....
BroadcastReceiver br = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// обрабатываем броадкаст
}
}
@Override
protected void onResume() {
super.onResume();
// создаем фильтр для BroadcastReceiver
IntentFilter intFilt = new IntentFilter("SOME_ACTIVITY_ACTION");
// регистрируем (включаем) BroadcastReceiver
LocalBroadcastManager.getInstance(this).registerReceiver(br, intFilt);
}
@Override
protected void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(this).unregisterReceiver(br);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question