首页 文章

android中的可点击小部件

提问于
浏览
60

开发人员文档似乎在这里失败了 . 我可以毫不犹豫地创建一个静态小部件,我甚至可以创建一个类似于模拟时钟小部件的小部件来更新自己,但是,我不能在我的生活中弄清楚如何创建一个小部件来响应用户点击的时间它 . 以下是开发人员文档为小部件活动应包含的内容提供的最佳代码示例(唯一的其他提示是API演示,仅创建静态小部件):

public class ExampleAppWidgetProvider extends AppWidgetProvider {
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        final int N = appWidgetIds.length;

        // Perform this loop procedure for each App Widget that belongs to this provider
        for (int i=0; i<N; i++) {
            int appWidgetId = appWidgetIds[i];

            // Create an Intent to launch ExampleActivity
            Intent intent = new Intent(context, ExampleActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

            // Get the layout for the App Widget and attach an on-click listener to the button
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider_layout);
            views.setOnClickPendingIntent(R.id.button, pendingIntent);

            // Tell the AppWidgetManager to perform an update on the current App Widget
            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }
}

来自:The Android Developer Documentation's Widget Page

因此,看起来在单击窗口小部件时调用挂起的意图,这是基于意图(我不太确定意图和挂起意图之间的区别是什么),并且意图是针对ExampleActivity类的 . 所以我让我的示例活动类成为一个简单的活动,在创建时,会创建一个mediaplayer对象,并启动它(它不会释放该对象,所以最终会崩溃,这是它的代码:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.sound);
    mp.start();
}

但是,当我将小部件添加到主屏幕并单击它时,实际上没有播放任何内容,当我将更新计时器设置为几百毫秒时(在appwidget提供程序xml文件中) . 此外,我设定了断点,并发现它不仅没有达到活动,而且我设置的断点也不会被触发 . (我仍然没有弄清楚为什么会这样),但是,logcat似乎表明正在运行活动类文件 .

那么,有什么办法可以让appwidget响应点击吗?因为onClickPendingIntent()方法是我发现的最接近onClick类型的方法 .

非常感谢你 .

2 回答

  • 125

    首先,添加一个带常量的静态变量 .

    public static String YOUR_AWESOME_ACTION = "YourAwesomeAction";
    

    然后,在将意图添加到挂起的intent之前,需要将操作添加到intent中:

    Intent intent = new Intent(context, widget.class);
    intent.setAction(YOUR_AWESOME_ACTION);
    

    (其中widget.class是AppWidgetProvider的类,您当前的类)

    然后,您需要使用getBroadcast创建PendingIntent

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
    

    为窗口小部件中的可单击视图设置onClickPendingIntent

    remoteView.setOnClickPendingIntent(R.id.widgetFrameLayout, pendingIntent);
    

    接下来,覆盖同一个类中的onReceive方法:

    @Override
    public void onReceive(Context context, Intent intent) {
     super.onReceive(context, intent);
    

    然后通过在onReceive方法中查询为您的操作返回的意图来响应您的按钮按下:

    if (intent.getAction().equals(YOUR_AWESOME_ACTION)) {
       //do some really cool stuff here
    }
    

    这应该做到!

  • 1

    保留 onUpdate 方法并将逻辑复制粘贴到 updateAppWidget .

    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                                int appWidgetId) {
    
        // Create an Intent to launch ExampleActivity when clicked
        Intent intent = new Intent(context, ExampleActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        // Construct the RemoteViews object
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider_layout);
        // Widgets allow click handlers to only launch pending intents
        views.setOnClickPendingIntent(R.id.button, pendingIntent);
        // Instruct the widget manager to update the widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
    
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // There may be multiple widgets active, so update all of them
        for (int appWidgetId : appWidgetIds) {
            updateAppWidget(context, appWidgetManager, appWidgetId);
        }
    }
    

    PS . PendingIntent 对于Intent来说只是"box",它允许其他应用程序访问并在您的应用程序中运行该Intent .

相关问题