如何从Android AppWidget启动Activity? [英] How to start Activity from Android AppWidget?

查看:606
本文介绍了如何从Android AppWidget启动Activity?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这样的代码效果很好.

    Intent configIntent = new Intent (context, WidgetConfigActivity.class);
    configIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    PendingIntent pIntent = PendingIntent.getActivity(context, 0, configIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    remoteView.setOnClickPendingIntent(R.id.btn, pIntent);

但是我想隐藏该按钮,因为活动将出现,所以我试图将意图发送给小部件本身,在onReceive()方法中执行隐藏组件,然后开始活动.问题是我无法在AppWidget中使用startActivity()函数.

But I want to hide that button befor activity will appear, so I'm triing to send intent to the widget itself, perform hiding components in onReceive() method and then start activity. Problem is that I can't use startActivity() function in AppWidget.

有什么解决办法吗?

推荐答案

问题是我无法在AppWidget中使用startActivity()函数.

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

是的,可以.您将Context对象传递到AppWidgetProvideronUpdate()(或onReceive())中–在该对象上调用startActivity().

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

这篇关于如何从Android AppWidget启动Activity?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆