S
S
svd712016-01-08 21:51:24
Android
svd71, 2016-01-08 21:51:24

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:

  1. Activity
  2. Service
  3. Receiver

So, when creating an activity, the receiver is registered. This is not very good, since I would like to register the receiver when creating the service, directly from the service. However, it throws a NullPointerException when calling Service.registerReceiver(). When searching the net, I found a solution - to move it from the Service.onCreate () function to a more prosperous code with a known context. Transferred to the activity and it went.
Again, when closing the activity with the system return key, it also throws a NullPointerException if the service is active and continues to run.
But as I understand it, the service is the main one and must remain in the system until the stop command comes. Yes, and all management should also be carried out from the service.
And so the questions:
1. in what sequence to create and register objects, in what methods;
2. in what sequence to destroy all these objects, in what methods;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem Vishnevsky, 2016-01-14
@Tema_man

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);
  }
}

Next, in the service, in the onStartCommand(...) method, we process the received intent, do what we need, send a broadcast, and complete our work by calling the stopSelf() method;
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;
        }
}

Now about activation. When creating an activity, we register the receiver in onResume() and subscribe it to the necessary broadcast from the service. In onPause we unlock the receiver.
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);
  }
}

In any case, we need more information from you. What do you want to get and fragments of the problematic code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question