문제

I basically want to make an intent and pass it to a service from my BroadcastReceiver's onReceive().

So far I always used View.getContext(), but here, I'm stuck. How exactly can I get the context so I can use public Intent (Context packageContext, Class<?> cls)?

도움이 되었습니까?

해결책

public abstract void onReceive(Context context, Intent intent)

onReceive gives you the context. What more do you want?

다른 팁

Well the Answer mentioned above is not of any use. You can use the context as long as you are in onReceive. once you code has returned from onReceive, the context is no longer existing.

So your problem statement say you wanted to start the service using this context in your intent creation and then calling startService with this context object. That cannot be done.

Read this what can and cannot be done in BroadcastReceiver context.

http://developer.android.com/reference/android/content/BroadcastReceiver.html

In the BroadcastReceiver the

onReceive(Context context, Intent intent)

method provides context

so

to start activity use

context.startActivity(intent);

and to start service use

context.startService(intent);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top