【问题标题】:Please help with very simple android widget button click. Getting very frustrated. :(请帮助非常简单的 android 小部件按钮单击。变得非常沮丧。 :(
【发布时间】:2011-02-10 23:22:49
【问题描述】:

我已经倾注了我能找到的每一个例子,并且我已经浏览了谷歌的官方文档。我要做的就是通过单击小部件的按钮创建一个 toast 通知。

我可以让小部件(和按钮)出现,但我无法触发我的意图。我已经看到了几个示例,这些示例显示了以不同的方式执行此操作,但我就是无法让它发挥作用。我已经很久没有对编码感到如此无助了,我开始感到愚蠢。

这是我目前所拥有的:

public class simpleclick extends AppWidgetProvider {
    /** Called when the activity is first created. */

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        final int N = appWidgetIds.length;

        Toast.makeText(context, "doing on update", Toast.LENGTH_SHORT).show();

        for (int i=0; i<N; i++) {

            int appWidgetId = appWidgetIds[i];

            Intent intent = new Intent(context, simpleclick.class);
            intent.setAction("ham");
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);
            views.setOnClickPendingIntent(R.id.Timm, pendingIntent);

            appWidgetManager.updateAppWidget(appWidgetId, views);

        }

    }

    //@Override
    public void onReceive(Context context, Intent intent) {
     // TODO Auto-generated method stub

     Toast.makeText(context, "action is: " + intent.getAction(), Toast.LENGTH_SHORT).show();

     super.onReceive(context, intent);

    }

}

我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.tblabs.simpleclick"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <receiver android:name="simpleclick">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data android:name="android.appwidget.provider" 
            android:resource="@xml/simpleclick" />
        </receiver>

    </application>
    <uses-sdk android:minSdkVersion="5" />

</manifest> 

我将不胜感激!

谢谢, 温迪

【问题讨论】:

  • 为了记录/调试目的,最好使用 Log 消息,它们会输出到 LogCat。无论如何,您使用 getActivity() 创建待处理的意图,而文档说 AppWidgetProvider 是广播接收器。那么我会尝试使用 getBroadcast()。

标签: android android-widget


【解决方案1】:

正如 bigstones 所写,您需要使用广播。 示例代码:

protected PendingIntent getPendingSelfIntent(Context context, String action) {
    Intent intent = new Intent(context, getClass());
    intent.setAction(action);
    return PendingIntent.getBroadcast(context, 0, intent, 0);
}

views.setOnClickPendingIntent(R.id.Timm, getPendingSelfIntent(context,
                              "ham"));

【讨论】:

    【解决方案2】:

    这里是完整版:

    package com.automatic.widget;
    
    import android.app.PendingIntent;
    import android.appwidget.AppWidgetManager;
    import android.appwidget.AppWidgetProvider;
    import android.content.ComponentName;
    import android.content.Context;
    import android.content.Intent;
    import android.widget.RemoteViews;
    
    public class Widget extends AppWidgetProvider {
    
        private static final String SYNC_CLICKED    = "automaticWidgetSyncButtonClick";
    
        @Override
        public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
            RemoteViews remoteViews;
            ComponentName watchWidget;
    
            remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
            watchWidget = new ComponentName(context, Widget.class);
    
            remoteViews.setOnClickPendingIntent(R.id.sync_button, getPendingSelfIntent(context, SYNC_CLICKED));
            appWidgetManager.updateAppWidget(watchWidget, remoteViews);
        }
    
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            super.onReceive(context, intent);
    
            if (SYNC_CLICKED.equals(intent.getAction())) {
    
                AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    
                RemoteViews remoteViews;
                ComponentName watchWidget;
    
                remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
                watchWidget = new ComponentName(context, Widget.class);
    
                remoteViews.setTextViewText(R.id.sync_button, "TESTING");
    
                appWidgetManager.updateAppWidget(watchWidget, remoteViews);
    
            }
        }
    
        protected PendingIntent getPendingSelfIntent(Context context, String action) {
            Intent intent = new Intent(context, getClass());
            intent.setAction(action);
            return PendingIntent.getBroadcast(context, 0, intent, 0);
        }
    }
    

    【讨论】:

    • 一开始你为什么打电话给super.onReceive
    猜你喜欢
    • 2012-05-14
    • 1970-01-01
    • 2011-07-19
    • 2012-04-02
    • 1970-01-01
    • 2020-11-21
    • 1970-01-01
    • 2012-01-02
    • 2011-08-25
    相关资源
    最近更新 更多