【问题标题】:Simple widget with a button to call an activity, or icons on home screen to call activities简单的小部件,带有一个按钮来调用活动,或主屏幕上的图标来调用活动
【发布时间】:2013-08-13 10:11:14
【问题描述】:

是否有任何解决方法可以直接从小部件上的按钮调用活动?就像一个漂亮的按钮来启动应用程序。

从文档和这里的一些答案来看,views.setOnClickPendingIntent 是唯一的方法,并且需要服务。但我不需要服务,因为我并没有真正更新小部件!

其实我最初的任务很简单。我想要一个调用活动的主屏幕上的图标,但我不希望该图标出现在应用程序抽屉中。我知道我可以在 app-drawer 中放置很多活动图标。

【问题讨论】:

    标签: android widget icons


    【解决方案1】:

    您不需要为此提供服务。

    您应该在 AppWidgetProvider 的 onUpdate 方法中设置 OnClickPendingIntent。

    这里有一些链接展示了它是如何完成的,它们基本相同,但有一些变化,你应该试试:

    Start activity by clicking on widget

    Launching activity from widget

    How do I run an Activity from a button on a widget in Android?

    How Do I Launch an Activity with Home Widget

    就像我说的,不需要服务。

    这是我使用的示例代码:

        @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){
    
         RemoteViews remoteViews=new RemoteViews(context.getPackageName(), R.layout.testwidget);
         //Intent set with the activity you want to start
         Intent intent = new Intent(context, MainActivity.class);
    
    
         PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
    
        remoteViews.setOnClickPendingIntent(R.id.textView1, pendingIntent);
    
        appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
        }
    

    【讨论】:

    • 谢谢,JanBo!我看到大多数例子都有“super.onUpdate(context, appWidgetManager, appWidgetIds);”在 onUpdate 的开始,但不是你的,这是好的做法吗?没有它,我看不到任何影响。
    • 嗯,包含它是一个好习惯,这里有一些解释 stackoverflow.com/questions/10860793/… 。就像你说的那样,没有它也可以工作,但总的来说这是不好的做法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-11
    • 1970-01-01
    • 1970-01-01
    • 2012-10-20
    • 1970-01-01
    相关资源
    最近更新 更多