2013-02-10 67 views
32

Tôi có một widget android tìm nạp dữ liệu từ máy chủ sau mỗi 10 phút và hiển thị nó trên màn hình.
Tôi muốn thêm nút "Làm mới" vào tiện ích đó.
Khi người dùng nhấp vào nút đó, tôi muốn chạy phương thức tìm nạp thông tin từ máy chủ.
Việc thêm trình xử lý sự kiện vào nút trong ứng dụng rất dễ dàng, tuy nhiên tôi không thể tìm thấy ví dụ cho tiện ích con.
Tôi muốn được trợ giúp thêm chức năng vào nút bấm trong một tiện ích.Sự kiện bấm nút cho widget android

+1

Đây là http://fackoverflow.com/a/8635715/2015318 hữu ích – StarsSky

Trả lời

39

tôi phát hiện ra làm thế nào để làm điều đó.
Thêm một hành động để các AndroidManifest.xml tập tin trong thẻ><receiver><intent-filter>:

<action android:name="MY_PACKAGE_NAME.WIDGET_BUTTON" /> 

Trong cung cấp thêm một hằng số khớp với tên action:

public static String WIDGET_BUTTON = "MY_PACKAGE_NAME.WIDGET_BUTTON"; 

Trong phương pháp onUpdate() thêm một ý định chưa xử lý khớp với hành động:

Intent intent = new Intent(WIDGET_BUTTON); 
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
views.setOnClickPendingIntent(R.id.MY_BUTTON_ID, pendingIntent); 

Cuối cùng, trong phương thức onRecieve(), hãy kiểm tra tên hành động :

if (WIDGET_BUTTON.equals(intent.getAction())) { 
//your code here 

    } 
+0

Cảm ơn bạn rất nhiều! –

+0

Cảm ơn @Sharon Haim Pour! –

+1

Tôi nghĩ rằng điều thú vị là bạn có thể (và có lẽ nên) sử dụng một mục đích rõ ràng thay vì ý định ngầm định. Điều này có nghĩa là bạn không phải xác định hành động trong tệp kê khai và rằng bạn nên tạo mục đích như sau: Intent intent = new Intent (context, MyClass.class); – user936580

9
protected PendingIntent getPendingSelfIntent(Context context, String action) { 
    Intent intent = new Intent(context, getClass()); 
    intent.setAction(action); 
    return PendingIntent.getBroadcast(context, 0, intent, 0); 
} 

views.setOnClickPendingIntent(R.id.Timm, getPendingSelfIntent(context, 
           "ham")); 

Cũng thích URL:

How to correctly handle click events on Widget

Nếu bạn giải quyết nó theo một cách khác, vui lòng cung cấp này như một câu trả lời

+1

Tôi không hiểu vị trí đặt mã này. Bạn gọi hàm trong nhấp chuột ở đâu? –

59

Dưới đây là một ví dụ nữa mà sẽ giúp:

package com.automatic.widget; 

import android.app.PendingIntent; 
import android.appwidget.AppWidgetManager; 
import android.appwidget.AppWidgetProvider; 
import android.content.ComponentName; 
import android.content.Context; 
import android.content.Intent; 
import android.widget.RemoteViews; 

public class Widget extends AppWidgetProvider { 

    private static final String SYNC_CLICKED = "automaticWidgetSyncButtonClick"; 

    @Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
     RemoteViews remoteViews; 
     ComponentName watchWidget; 

     remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout); 
     watchWidget = new ComponentName(context, Widget.class); 

     remoteViews.setOnClickPendingIntent(R.id.sync_button, getPendingSelfIntent(context, SYNC_CLICKED)); 
     appWidgetManager.updateAppWidget(watchWidget, remoteViews); 
    } 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     // TODO Auto-generated method stub 
     super.onReceive(context, intent); 

     if (SYNC_CLICKED.equals(intent.getAction())) { 

      AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); 

      RemoteViews remoteViews; 
      ComponentName watchWidget; 

      remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout); 
      watchWidget = new ComponentName(context, Widget.class); 

      remoteViews.setTextViewText(R.id.sync_button, "TESTING"); 

      appWidgetManager.updateAppWidget(watchWidget, remoteViews); 

     } 
    } 

    protected PendingIntent getPendingSelfIntent(Context context, String action) { 
     Intent intent = new Intent(context, getClass()); 
     intent.setAction(action); 
     return PendingIntent.getBroadcast(context, 0, intent, 0); 
    } 
} 
+2

Cảm ơn Ví dụ tốt như vậy Tôi đã sử dụng mã này và ứng dụng của tôi Làm việc tốt .... Thnks a Lots –

+0

Cảm ơn một tấn, câu trả lời này đã giúp tôi. =) –

+1

Mã này hoạt động mà không có sự nhầm lẫn các ví dụ khác được trình bày - đây là một ví dụ tuyệt vời, ngắn gọn về thông tin hướng dẫn dev - cảm ơn! – headscratch

7

Dưới đây là một câu trả lời với các lợi ích sau:

  • Nó xử lý tất cả các trường App Widget (một người sử dụng có thể có nhiều phiên bản của tiện ích con trong các cấu hình/kích thước khác nhau trên màn hình của bạn). Mã hóa cho tất cả các phiên bản là tài liệu chính thức quy định. Xem Guide > App Widgets > Using the AppWidgetProvider Class, cuộn xuống ví dụ về mã cho "ExampleAppWidgetProvider".
  • Mã Workhorse trong onReceive trong các cuộc gọi có hiệu lực onUpdate (để bạn giảm trùng lặp mã).
  • Mã trong onUpdate(Context context) được tổng quát để mã có thể được thả vào bất kỳ lớp con AppWidgetProvider nào.

Mã:

public class MyWidget extends AppWidgetProvider { 

    private static final String ACTION_UPDATE_CLICK = 
       "com.example.myapp.action.UPDATE_CLICK"; 

    private static int mCount = 0; 

    private static String getMessage() { 
     return String.valueOf(mCount++); 
    } 

    private PendingIntent getPendingSelfIntent(Context context, String action) { 
     // An explicit intent directed at the current class (the "self"). 
     Intent intent = new Intent(context, getClass()); 
     intent.setAction(action); 
     return PendingIntent.getBroadcast(context, 0, intent, 0); 
    } 

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

     String message = getMessage(); 

     // Loop for every App Widget instance that belongs to this provider. 
     // Noting, that is, a user might have multiple instances of the same 
     // widget on 
     // their home screen. 
     for (int appWidgetID : appWidgetIds) { 
      RemoteViews remoteViews = new RemoteViews(context.getPackageName(), 
                 R.layout.my_widget); 

      remoteViews.setTextViewText(R.id.textView_output, message); 
      remoteViews.setOnClickPendingIntent(R.id.button_update, 
               getPendingSelfIntent(context, 
                  ACTION_UPDATE_CLICK) 
      ); 

      appWidgetManager.updateAppWidget(appWidgetID, remoteViews); 

     } 
    } 

    /** 
    * A general technique for calling the onUpdate method, 
    * requiring only the context parameter. 
    * 
    * @author John Bentley, based on Android-er code. 
    * @see <a href="http://android-er.blogspot.com 
    * .au/2010/10/update-widget-in-onreceive-method.html"> 
    * Android-er > 2010-10-19 > Update Widget in onReceive() method</a> 
    */ 
    private void onUpdate(Context context) { 
     AppWidgetManager appWidgetManager = AppWidgetManager.getInstance 
       (context); 

     // Uses getClass().getName() rather than MyWidget.class.getName() for 
     // portability into any App Widget Provider Class 
     ComponentName thisAppWidgetComponentName = 
       new ComponentName(context.getPackageName(),getClass().getName() 
     ); 
     int[] appWidgetIds = appWidgetManager.getAppWidgetIds(
       thisAppWidgetComponentName); 
     onUpdate(context, appWidgetManager, appWidgetIds); 
    } 

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

     if (ACTION_UPDATE_CLICK.equals(intent.getAction())) { 
      onUpdate(context); 
     } 
    } 

} 

Các widget trông như thế này

Widget update button example. Simple counting.

này được xây dựng trên getPendingSelfIntent công việc của @Kels, @SharonHaimPour và @ Erti-ChrisEelmaa.

Nó cũng được xây dựng trên Android-er > 2010-10-19 > Update Widget in onReceive() method (không phải tôi), nơi nó được thể hiện cách gọi onUpdate từ onReceive, trên cơ sở phiên bản ứng dụng Widget. Tôi làm cho mã nói chung và bọc nó trong callOnUpdate.

Các vấn đề liên quan