如何从 Widget、Android、Java 获取值开关(开/关)

问题描述 投票:0回答:0

我正在开发一个 widget 用于通过开关打开/关闭

小部件包含TextViewSwitch

你可以看看下面我到目前为止所做的代码

小部件类继承自 AppWidgetProvider

TimeWidget
班级

package com.example.time;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;

import android.content.Intent;
import android.widget.RemoteViews;

public class TimeWidget extends AppWidgetProvider {
private static final String mySwitch = "MySwitch";

@Override
public void onUpdate(Context context, AppWidgetManager 
appWidgetManager, int[] appWidgetIds) {

    for (int appWidgetId : appWidgetIds) {

        RemoteViews views = new RemoteViews(context.getPackageName(), 
        R.layout.time_widget);

        PendingIntent pending = getPendingSelfIntent(context);
         
        RemoteViews.RemoteResponseresponse=
        RemoteViews.RemoteResponse.fromPendingIntent(pending);

        views.setOnCheckedChangeResponse(R.id.appwidget_switch, 
        response);

        views.addView(R.id.appwidget_switch ,views);

        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}

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

    }
}

getPendingSelfIntent
方法

protected PendingIntent getPendingSelfIntent(Context context) {
    Intent intent = new Intent(context, getClass());
    intent.setAction(mySwitch);
    return PendingIntent.getBroadcast(context, 0, intent, 
    PendingIntent.FLAG_UPDATE_CURRENT |PendingIntent.FLAG_IMMUTABLE);
    }
}

我的问题:我想知道--->我怎样才能从小部件中获得价值开关,

我搜索了这个问题的解决方案,但没有找到。

谁能解释一下我该如何解决我的问题?

java android widget switch-statement android-appwidget
© www.soinside.com 2019 - 2024. All rights reserved.