Get started with BroadcastReceiver

In my application, I register BroadcastReceiverin the method onCreate()for Service.

registerReceiver(receiver, newIntentFilter(myAction));

Now I need to start the activity from the newly registered BroadcastReceivereach time onReceive(Context context, Intent intent):

Intent i = new Intent(context,MyClass.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);

"Context" is that Contextwhich has been conveyed to mine BroadcastReceiver. It successfully worked and started Activity. Is this the correct and reliable way to start Activityinside a BraodcastReceiverfrom a Service? Because there is a lot to receive Context, for example getApplicationContext()or getApplication()etc.

In my situation is used Context, which was correctly transferred to mine BroadcastReceiver?

+4
2

"startActivity()".

- :

public void startAct() {
    Intent i = new Intent();
    i.setClass(this, MyActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(i);
}

BroadcastReceiver.

android.app.Service android.app.Context, startActivity. , , FLAG_ACTIVITY_NEW_TASK .

, .

+3

"". Context, onReceive(), . , :

context.getApplicationContext().startActivity(i);

. .

+2

All Articles