簡體   English   中英

如何使用主屏幕小部件上的按鈕?

[英]How to use an button on homescreen widget?

我有一個小部件,帶有一個按鈕和一個圖像按鈕。 我希望如果您單擊圖像按鈕,則會打開一個帶有編輯文本的透明活動。 例如我們填寫 01 ,當你點擊普通按鈕時,它會加載一個 webview ,顯示 html 文件 01.html。 這可能嗎?

我的代碼是:

 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: ERROR/AndroidRuntime(381): FATAL EXCEPTION: main 07-09 14:19:31.070: ERROR/AndroidRuntime(381): java.lang.RuntimeException: Unable to instantiate receiver com.liedboek. widget.aantekening: java.lang.ClassCastException: com.liedboek.widget.aantekening 07-09 14:19:31.070: ERROR/AndroidRuntime(381): at android.app.ActivityThread.handleReceiver(ActivityThread.java:1773) 07- 09 14:19:31.070: ERROR/AndroidRuntime(381): at android.app.ActivityThread.access$2400(ActivityThread.java:117) 07-09 14:19:31.070: ERROR/AndroidRuntime(381): at 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: ERROR/AndroidRuntime(381): at android.os.Looper.loop(Looper.java:123) 07-09 14:19:31.070 : ERROR/AndroidRuntime(381): at android.app.ActivityThread.main(ActivityThread.java:3683) 07-09 14:19:31.070: ERROR/AndroidRuntime(381): at java.lang.reflect.Method.invokeNative( Native Method) 07-09 14:19:31.070: ERROR/AndroidRuntime(381): at java.lang.reflect.Method.invoke(Method.java:507) 07-09 14:19:31.070: ERROR/AndroidRuntime(381 ): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 07-09 14:19:31.070: ERROR/AndroidRuntime(381): at com.ZC31B32364CE19CA8FCD1 50A417ECCE58Z.internal.os.ZygoteInit.main(ZygoteInit.java:597) 07-09 14:19:31.070: 錯誤/AndroidRuntime(381): 在 dalvik.system.NativeStart.main(Native Method) 07:-0914:-19 :31.070: ERROR/AndroidRuntime(381): Caused by: java.lang.ClassCastException: com.liedboek.widget.aantekening 07-09 14:19:31.070: ERROR/AndroidRuntime(381): at android.app.ActivityThread.handleReceiver (ActivityThread.java:1764) 07-09 14:19:31.070: ERROR/AndroidRuntime(381): ... 10 more 07-09 14:19:31.101: WARN/ActivityManager(69): Process com.liedboek.widget已經崩潰太多次了:殺!

但我的代碼是另一個代碼,也是一個按鈕小部件:

package 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); 



  } 
 }

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM