How to start Activity from Android AppWidget?

后端 未结 3 1280
眼角桃花
眼角桃花 2021-01-11 13:48

Code like this works well.

    Intent configIntent = new Intent (context, WidgetConfigActivity.class);
    configIntent.putExtra(AppWidgetManager.EXTRA_APPWI         


        
相关标签:
3条回答
  • 2021-01-11 14:24

    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.

    0 讨论(0)
  • 2021-01-11 14:26
       // 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);
    
    0 讨论(0)
  • 2021-01-11 14:27

    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.

    0 讨论(0)
提交回复
热议问题