Android-widget小组件点击事件响应

添加应用小组件,实现响应小组件点击事件

static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
        SimpleDateFormat mFormat = new SimpleDateFormat("MM-dd HH:mm:ss");
        CharSequence widgetText = mFormat.format(new Date(System.currentTimeMillis()));
        // Construct the RemoteViews object
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
        views.setTextViewText(R.id.widget_time, widgetText);

        PendingIntent logoPIntent = PendingIntent.getActivity(context, 0,
                new Intent(context, getActivityClass()), PendingIntent.FLAG_UPDATE_CURRENT);
        views.setOnClickPendingIntent(R.id.logo_img, logoPIntent);   //点击跳转

        Intent serviceIntent = new Intent(context, GridWidgetService.class);
        serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        serviceIntent.setData(Uri.parse(serviceIntent.toUri(Intent.URI_INTENT_SCHEME)));
        views.setRemoteAdapter(R.id.gridview, serviceIntent);

        Intent clickIntent = new Intent(context, NewAppWidget.class);
        clickIntent.setAction("ITEM_VIEW_CLICK_ACTION");
        clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        clickIntent.setData(Uri.parse(clickIntent.toUri(Intent.URI_INTENT_SCHEME)));
        PendingIntent pendingIntentTemplate = PendingIntent.getBroadcast(
                context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        views.setPendingIntentTemplate(R.id.gridview, pendingIntentTemplate);

        Intent serviceIntent1 = new Intent(context, ListWidgetService.class);
        serviceIntent1.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        serviceIntent1.setData(Uri.parse(serviceIntent1.toUri(Intent.URI_INTENT_SCHEME)));
        views.setRemoteAdapter(R.id.listview, serviceIntent1);

        Intent clickIntent1 = new Intent(context, NewAppWidget.class);
        clickIntent1.setAction("ITEM_VIEW_CLICK_ACTION");
        clickIntent1.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        clickIntent1.setData(Uri.parse(clickIntent1.toUri(Intent.URI_INTENT_SCHEME)));
        PendingIntent pendingIntentTemplate1 = PendingIntent.getBroadcast(
                context, 0, clickIntent1, PendingIntent.FLAG_UPDATE_CURRENT);
        views.setPendingIntentTemplate(R.id.listview, pendingIntentTemplate1);

        Intent refreshIntent = new Intent(context, NewAppWidget.class);
        refreshIntent.setAction("REFRESH_ACTION");
        ComponentName componentName = new ComponentName(context, NewAppWidget.class);
        refreshIntent.setComponent(componentName);
        PendingIntent refreshPIntent = PendingIntent.getBroadcast(context, 0,
                refreshIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        views.setOnClickPendingIntent(R.id.refresh_img, refreshPIntent);   //点击启动服务
        // Instruct the widget manager to update the widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
private static Class getActivityClass(){
        Class clz = null;
        try {
            String activityName = "com.test.Android.TestActivity";
            clz = Class.forName(activityName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return clz;
    }
@Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        String action = intent.getAction();
        switch (action){
            case "REFRESH_ACTION":
                startRefresh(context,false);
                break;
            case "ITEM_VIEW_CLICK_ACTION":
                String stkInfo = intent.getStringExtra("StkInfo");
                int index = intent.getIntExtra("index",0);
                Intent jumpIntent = getJumpIntent(context,stkInfo,index);
                context.startActivity(jumpIntent);
                break;
            default:
                //接收广播,自动刷新
                startRefresh(context,true);
                break;
        }
    }

1.点击可拉起应用进入到指定页面

2.GridView,ListView点击item可拉起应用进入到对应详情页面(设置intent模板,在RemoteViewsService设置填充信息,绑定item信息,接收广播解析intent获取item绑定信息)

3.点击可给小组件自身发送广播,在接收广播的地方处理具体事务

你可能感兴趣的:(android,java)