【问题标题】:Advanced Widgets高级小部件
【发布时间】:2013-08-15 03:37:36
【问题描述】:

我仍然无法在小部件中进行高级操作。假设我有一种方法可以执行诸如打开手电筒之类的操作。我如何使用任何可能的方式从小部件中调用此方法,例如 Async 、 services 。 .. 我的代码(小部件):

public class QFlashlightWidgetProvider 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 QFlashlightActivity
        Intent intent = new Intent(context, ScreenLight.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.qflashlight_appwidget);
        views.setOnClickPendingIntent(R.id.btn1, pendingIntent);

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

请支持我的任何代码,至少可以在单击小部件时调用 toast 消息。这只是一个小部件,一旦单击就会打开我的活动。

【问题讨论】:

  • 不,它已经是可点击的了,但我怎么能做我只能在活动中做的事情,比如在点击小部件时显示敬酒消息。请以任何可能的方式支持我。
  • this 有帮助吗?
  • This link 很有帮助,我之前尝试过,但我不知道这次为什么有效,但现在我遇到了另一个问题。我在负责的方法之前放了一条祝酒词打开/关闭手电筒。我第一次单击小部件时,会弹出一个 toast 消息,第二次单击它时 LED 会亮起,它只是崩溃,甚至在崩溃之前不显示 toast 消息
  • 当您的应用程序崩溃时,只需检查 logcat 即可找出原因。它会给你原因和行号。

标签: android eclipse android-intent service widget


【解决方案1】:

我已经解决了这个问题。问题是程序在它已经打开时打开了gthe led。

What the program does?

我第一次点击小部件时,led 会打开,布尔值变为 false。

Problem

程序将布尔值设为 false 简单,因为它不是 static

【讨论】:

    猜你喜欢
    • 2010-12-10
    • 1970-01-01
    • 2011-04-13
    • 2019-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-03
    • 1970-01-01
    相关资源
    最近更新 更多