如何在主屏幕小部件中使用按钮,如活动

时间:2013-09-20 04:53:31

标签: android android-appwidget

我有一个带按钮,文本View和DataBase的小部件。 数据库已经有了一些数据 textView显示数据库中的第一个数据 当我点击按钮时,我想要更改文本视图并显示下一个数据 例如我的DB是这样的:

          {(id=1,name="First")(id=2,name="second")(id=3,name="third")}

        textview="First"

点击按钮后

       textview="second"

这是我的代码

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    super.onUpdate(context, appWidgetManager, appWidgetIds);
    for(int i=0;i<appWidgetIds.length;i++){
        int appWidgetId=appWidgetIds[i];
      ArrayList<String> list = new ArrayList<String>();
      DataBaseHelper db = new DataBaseHelper(context);
            RemoteViews(context.getPackageName(),R.layout.activity_main);
            RemoteViews rmtview = new RemoteViews(context.getPackageName(),R.layout.widget);
            list=db.getData(1);
            String s=list.get(GET)
            list=db.getData(1);
            rmtview.setTextViewText(R.id.textView1, s);
            db.close();

修改

appwidet提供商

public class widget extends AppWidgetProvider {

public static String MyACTION = "MyAction";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    // TODO Auto-generated method stub
    super.onUpdate(context, appWidgetManager, appWidgetIds);

RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
    remoteViews.setOnClickPendingIntent(R.id.button3, buildButtonPendingIntent(context));


    for(int i=0;i<appWidgetIds.length;i++){
        int appWidgetId=appWidgetIds[i];
    ArrayList<String> list = new ArrayList<String>();
    DataBaseHelper db = new DataBaseHelper(context);
    RemoteViews rmtview = new RemoteViews(context.getPackageName(),R.layout.widget);
    list=db.getData(1);
    String s=list.get(0);
    rmtview.setTextViewText(R.id.textView1, s);
    db.close();

    ///////////////////////



    ///////////////////////
    appWidgetManager.updateAppWidget(appWidgetId, rmtview);

   // RemoteViews un = new RemoteViews(context.getPackageName(),R.layout.widget);
    }


}





public static PendingIntent buildButtonPendingIntent(Context context) {
    Log.d("me","pending entent launched");
    Intent intent = new Intent();
   intent.setAction("pl.looksok.intent.action.CHANGE_TEXT");
    return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}



public static void pushWidgetUpdate(Context context, RemoteViews remoteViews) {
    ComponentName myWidget = new ComponentName(context,
            widget.class);
    Log.d("me","push launched");
    AppWidgetManager manager = AppWidgetManager.getInstance(context);
    manager.updateAppWidget(myWidget, remoteViews);

}



}

MyWidgetIntentReceiver.java

public class MyWidgetIntentReceiver extends BroadcastReceiver {

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

Log.d("me", "OnRecive ...");
    if(intent.getAction().equals("pl.looksok.intent.action.CHANGE_TEXT")){
updateWidgetPictureAndButtonListener(context);
        }

}
public static void updateWidgetPictureAndButtonListener(Context context) {

Log.d("me", "update widget... ");
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
    // ..Get Text from DB
        //
    remoteViews.setTextViewText(R.id.textView1, "...text");

    //REMEMBER TO ALWAYS REFRESH YOUR BUTTON CLICK LISTENERS!!!
    remoteViews.setOnClickPendingIntent(R.id.button3, widget.buildButtonPendingIntent(context));

    widget.pushWidgetUpdate(context.getApplicationContext(), remoteViews);

}
}

EDIT2

我删除了所有内容

    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    // TODO Auto-generated method stub
    super.onUpdate(context, appWidgetManager, appWidgetIds);
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget);
    remoteViews.setOnClickPendingIntent(R.id.button3, buildButtonPendingIntent(context));
    appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}

1 个答案:

答案 0 :(得分:1)

您需要在按钮点击事件上实现广播接收器。 何时单击按钮。它将广播&amp;接收者抓住这个动作。 在Broadcast Receiver中实现从DB获取数据。

检查Tutorial

onUpdate()
{
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widgetview);
        remoteViews.setOnClickPendingIntent(R.id.toggleButton1, buildButtonPendingIntent(context));

}
public static PendingIntent buildButtonPendingIntent(Context context) {
        Intent intent = new Intent();
       intent.setAction("pl.looksok.intent.action.CHANGE_TEXT");

        return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    }

修改

 public static void pushWidgetUpdate(Context context, RemoteViews remoteViews) {
            ComponentName myWidget = new ComponentName(context, MyWidgetProvider.class);
            AppWidgetManager manager = AppWidgetManager.getInstance(context);
            manager.updateAppWidget(myWidget, remoteViews);     
        }

现在,在清单

中注册
 <receiver
            android:name=".MyWidgetIntentReceiver"
            android:label="widgetBroadcastReceiver" >
            <intent-filter>
                <action android:name="pl.looksok.intent.action.CHANGE_TEXT" />

            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/demo_widget_provider" />
        </receiver>

现在MyWidgetIntentReceiver

public class MyWidgetIntentReceiver extends BroadcastReceiver {

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


        if(intent.getAction().equals("pl.looksok.intent.action.CHANGE_TEXT")){
updateWidgetPictureAndButtonListener(context);
            }

}
public static void updateWidgetPictureAndButtonListener(Context context) {

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widgetview);
        // ..Get Text from DB
            //
        remoteViews.setTextViewText(R.id.widgettext, "...text");

        //REMEMBER TO ALWAYS REFRESH YOUR BUTTON CLICK LISTENERS!!!
        remoteViews.setOnClickPendingIntent(R.id.toggleButton1, MyWidgetProvider.buildButtonPendingIntent(context));

        MyWidgetProvider.pushWidgetUpdate(context.getApplicationContext(), remoteViews);

    }
}