【问题标题】:How to use an button on homescreen widget?如何使用主屏幕小部件上的按钮?
【发布时间】:2011-09-28 20:47:24
【问题描述】:

我有一个小部件,带有一个按钮和一个图像按钮。 我希望如果您单击图像按钮,则会打开一个带有编辑文本的透明活动。 比如我们填01 当您单击普通按钮时,它将加载一个显示 html 文件 01.html 的 web 视图。 这可能吗?

我的代码是:

 package com.kerk.liedboek.widget;

 import android.app.Notification;
 import android.app.NotificationManager;
 import android.app.PendingIntent;
 import android.appwidget.AppWidgetManager;
 import android.appwidget.AppWidgetProvider;
 import android.content.Context;
 import android.content.Intent;
 import android.util.Log;
 import android.widget.RemoteViews;
 import android.widget.Toast;

  public class main extends AppWidgetProvider {

      public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
      public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";

      @Override 
      public void onUpdate(Context context, AppWidgetManager 
 appWidgetManager, 
        int[] appWidgetIds) { 
            Intent i = new Intent(context, edit.class); 
            PendingIntent pi = PendingIntent.getBroadcast(context,0, i,0); 
            RemoteViews rv = new RemoteViews(context.getPackageName(), 
 R.layout.main); 
            rv.setOnClickPendingIntent(R.id.button_one, pi); 

            Intent i2 = new Intent(context, webview.class); 
            PendingIntent pi2 = PendingIntent.getBroadcast(context,0, i2,0); 
            RemoteViews rv2 = new RemoteViews(context.getPackageName(), 
 R.layout.main); 
            rv2.setOnClickPendingIntent(R.id.button_two, pi2); 

    }
}

我是 android 新手,现在我是新手。但我希望你能理解我,你可以帮助我。 高威

编辑:

我的日志是:

07-09 14:19:31.070:错误/AndroidRuntime(381):致命异常:主要 07-09 14:19:31.070: 错误/AndroidRuntime(381): java.lang.RuntimeException: 无法实例化接收器 com.liedboek.widget.aantekening: java.lang.ClassCastException: com.liedboek.widget.aantekening 07-09 14:19:31.070: 错误/AndroidRuntime(381): 在 android.app.ActivityThread.handleReceiver(ActivityThread.java:1773) 07-09 14:19:31.070: 错误/AndroidRuntime(381): 在 android.app.ActivityThread.access$2400(ActivityThread.java:117) 07-09 14:19:31.070: 错误/AndroidRuntime(381): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:981) 07-09 14:19:31.070: 错误/AndroidRuntime(381): 在 android.os.Handler.dispatchMessage(Handler.java:99) 07-09 14:19:31.070: 错误/AndroidRuntime(381): 在 android.os.Looper.loop(Looper.java:123) 07-09 14:19:31.070: 错误/AndroidRuntime(381): 在 android.app.ActivityThread.main(ActivityThread.java:3683) 07-09 14:19:31.070: 错误/AndroidRuntime(381): 在 java.lang.reflect.Method.invokeNative(Native Method) 07-09 14:19:31.070: 错误/AndroidRuntime(381): 在 java.lang.reflect.Method.invoke(Method.java:507) 07-09 14:19:31.070: 错误/AndroidRuntime(381): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 07-09 14:19:31.070: 错误/AndroidRuntime(381): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 07-09 14:19:31.070:错误/AndroidRuntime(381):在 dalvik.system.NativeStart.main(本机方法) 07-09 14:19:31.070: 错误/AndroidRuntime(381): 原因: java.lang.ClassCastException: com.liedboek.widget.aantekening 07-09 14:19:31.070: 错误/AndroidRuntime(381): 在 android.app.ActivityThread.handleReceiver(ActivityThread.java:1764) 07-09 14:19:31.070: 错误/AndroidRuntime(381): ... 还有 10 个 07-09 14:19:31.101: WARN/ActivityManager(69): 进程 com.liedboek.widget 崩溃了太多次:杀死!

但我的代码是另一个代码,也是一个按钮小部件:

包 com.liedboek.widget;

 import android.app.Activity;
 import android.app.PendingIntent; 
 import android.appwidget.AppWidgetManager;
 import android.appwidget.AppWidgetProvider;
 import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
 import android.widget.RemoteViews;

 public class main extends AppWidgetProvider {

public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";

@Override 
public void onUpdate(Context context, AppWidgetManager 
appWidgetManager, 
  int[] appWidgetIds) { 
    // Create an Intent to launch ExampleActivity
    Intent intent = new Intent(context, aantekening.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.main);
    views.setOnClickPendingIntent(R.id.button_one, pendingIntent);

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



  } 
 }

【问题讨论】:

    标签: android widget


    【解决方案1】:

    您可能想要使用getActivity,而不是使用getBroadcast

    【讨论】:

    • 我不懂你。你能在网上给我一个例子吗?或者你能给我一个代码吗?
    • 拜托,阅读你粘贴的代码(我猜你没有写它或什么的)......然后你就会明白;)
    • 我把它放在我的代码中,但是当我点击一个按钮时,什么也没有。我不明白为什么?
    • 哦,伙计……你太懒了……如果你写了代码,你就知道你写了这个:PendingIntent.getBroadcast,它允许你在用户点击小部件项目时发送广播消息。但是,您要启动一个 Activity,因此您必须改用 PendingIntent.getActivity
    • 好吧,我很抱歉,我现在了解你,但我现在不知道如何启动它。我有:Intent i = new Intent(context, edit.class); PendingIntent pi = PendingIntent.getActivity(context, zoek.class);但这也不起作用:它说:PendingIntent 类型中的方法 getActivity(Context, int, Intent, int) 不适用于参数 (Context, int, Class, int) RemoteViews rv = newRemoteViews( context.getPackageName(), R.layout.configure); rv.setOnClickPendingIntent(R.id.button_one, pi);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多