How to start Activity from Android AppWidget?

你离开我真会死。 提交于 2019-12-01 02:54:25

Problem is that I can't use startActivity() function in AppWidget.

Yes, you can. You are passed in a Context object into onUpdate() (or onReceive()) of your AppWidgetProvider -- call startActivity() on that.

Thanks 2 CommonsWare

There is one more thing to do. context.startActivity(); throws RuntimeException in this case.

Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

So you need to set flag

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

before.

sandhu
   // on receive function use this for new activity start
               Intent intent = new Intent (context, AppWdget.class);
               intent.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK);
               context.startActivity (intent);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!