Reputation: 427
Hi In my android app background process,I need to call the intent of my class.But my activity gets opened when i pause the app by going to task manager.Please help me to solve this problem.
Here is my code:
intent = new Intent(Service.this,PushNotification.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
If i didnt set the flag(new_Task) my app force close and i got exception. Here is my error in logcat:
01-16 16:23:44.139: E/AndroidRuntime(18251): FATAL EXCEPTION: main
01-16 16:23:44.139: E/AndroidRuntime(18251): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
01-16 16:23:44.139: E/AndroidRuntime(18251): at android.app.ContextImpl.startActivity(ContextImpl.java:847)
01-16 16:23:44.139: E/AndroidRuntime(18251): at android.content.ContextWrapper.startActivity(ContextWrapper.java:276)
01-16 16:23:44.139: E/AndroidRuntime(18251): at com.app.Service.MobilyzerService$ServerTask.onPostExecute(MobilyzerService.java:359)
01-16 16:23:44.139: E/AndroidRuntime(18251): at com.app.Service.MobilyzerService$ServerTask.onPostExecute(MobilyzerService.java:1)
01-16 16:23:44.139: E/AndroidRuntime(18251): at android.os.AsyncTask.finish(AsyncTask.java:602)
01-16 16:23:44.139: E/AndroidRuntime(18251): at android.os.AsyncTask.access$600(AsyncTask.java:156)
01-16 16:23:44.139: E/AndroidRuntime(18251): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
01-16 16:23:44.139: E/AndroidRuntime(18251): at android.os.Handler.dispatchMessage(Handler.java:99)
01-16 16:23:44.139: E/AndroidRuntime(18251): at android.os.Looper.loop(Looper.java:137)
01-16 16:23:44.139: E/AndroidRuntime(18251): at android.app.ActivityThread.main(ActivityThread.java:4441)
01-16 16:23:44.139: E/AndroidRuntime(18251): at java.lang.reflect.Method.invokeNative(Native Method)
01-16 16:23:44.139: E/AndroidRuntime(18251): at java.lang.reflect.Method.invoke(Method.java:511)
01-16 16:23:44.139: E/AndroidRuntime(18251): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-16 16:23:44.139: E/AndroidRuntime(18251): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-16 16:23:44.139: E/AndroidRuntime(18251): at dalvik.system.NativeStart.main(Native Method)
Please guide me.
Upvotes: 0
Views: 1982
Reputation: 2824
For opening an Activity
using intent please use startActivity
Code will be
Intent inda = new Intent(getApplicationContext(),
NewDutyActivity.class);
inda.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
inda.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(inda);
i think you are trying to open the activity from a service
as you are trying to start a UI component from something that is not it self a UI component you are getting this error.
For that reason you have to add new task flag
in your intent.
Upvotes: 0
Reputation: 4470
my solution is
intent = new Intent(Service.this,PushNotification.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity( intent );
Upvotes: 1
Reputation: 21201
if you want to call intent in service then
call the intent as this.startService(intent);
(or)
if you want ot call the intent from activity then call it as startActivity(intent);
Upvotes: 1