2017-09-03 73 views
1

我有顯示簡單的文本和3個按鈕的窗口小部件:處理多個按鈕的小窗口內 - 的Android

  • 刷新(精選另一個隨機字符串並把它顯示)
  • 複製(複製的內容的TextView到剪貼板)
  • 分享(分享的TextView到

我已經得到了刷新按鈕的設置和工作得很好的社交媒體和等)的內容,但我似乎無法想出一個辦法處理另兩個按鈕

PS。我不需要實際的代碼我已經知道該怎麼做複製和分享我只需要知道

這裏點擊如何處理事件,是迄今爲止我的代碼:

Button copy_content; 
Button share_content; 

void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) { 
    /** Code below will be executed once the timer is over*/ 
    String widgetText = RandQuotes[rand.nextInt(RandQuotes.length)]; 

    // Construct the RemoteViews object 
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.quotes_widget); 
    views.setTextViewText(R.id.sayings, widgetText); 



    // Instruct the widget manager to update the widget 
    appWidgetManager.updateAppWidget(appWidgetId, views); 
} 

@Override 
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
    // There may be multiple widgets active, so update all of them 
    for (int appWidgetId : appWidgetIds) { 
     updateAppWidget(context, appWidgetManager, appWidgetId); 
    } 
    final int count = appWidgetIds.length; 

    for (int i = 0; i < count; i++) { 
     int widgetId = appWidgetIds[i]; 
     String on_sayings = RandQuotes[rand.nextInt(RandQuotes.length)]; 

     RemoteViews remoteViews = new RemoteViews(context.getPackageName(), 
       R.layout.quotes_widget); 
     remoteViews.setTextViewText(R.id.sayings, on_sayings); 

     Intent intent = new Intent(context, HavamalWidget.class); 
     intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); 
     intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 
       0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
     remoteViews.setOnClickPendingIntent(R.id.switch_trigger, pendingIntent); 
     appWidgetManager.updateAppWidget(widgetId, remoteViews); 
    } 

} 

回答

0

剛用動作在按鈕上做出待定意圖。所以在onReceive()只是檢查意圖的行爲,並就此做出一些邏輯。如果你做了一些長期的任務,最好在IntentService中做所有的邏輯。在你的按鈕

設置掛起的意圖是在RemoteViews這樣的:

public static final String ACTION_BUTTON_SHARE = "ACTION_BUTTON_SHARE"; 
public static final String ACTION_BUTTON_REFRESH = "ACTION_BUTTON_REFRESH"; 

Intent refreshIntent = new Intent(context, ExampleAppWidget.class) 
      .setAction(ACTION_BUTTON_REFRESH); 
PendingIntent refreshPI = PendingIntent.getBroadcast(context, 0, refreshIntent, 
      PendingIntent.FLAG_UPDATE_CURRENT); 
remoteViews.setOnClickPendingIntent(R.id.refresh_button, refreshPI); 

Intent shareIntent = new Intent(context, ExampleAppWidget.class) 
      .setAction(ACTION_BUTTON_SHARE); 
PendingIntent sharePI = PendingIntent.getBroadcast(context, 0, shareIntent, 
      PendingIntent.FLAG_UPDATE_CURRENT); 
remoteViews.setOnClickPendingIntent(R.id.share_button, refreshPI); 

在ExampleAppWidget.class(從的AppWidgetProvider延伸)覆蓋的onReceive()方法

@Override 
public void onReceive(Context context, Intent intent) { 
    if (intent != null) { 
     if (intent.getAction().equals(ACTION_BUTTON_REFRESH)) { 
      //make some logic here on button refresh 
     } else if (intent.getAction().equals(ACTION_BUTTON_SHARE)) { 
      //make some logic here on button share 
     } else { 
      super.onReceive(context, intent); 
     } 
    } 
} 
+0

而下面我迷路了你的指示,你可以發佈完整的答案嗎? –

+0

只是再次檢查答案 –

+0

我遇到了一些'CommonRemoteViewBuilder'的麻煩,並且操作按鈕'ACTION_BUTTON_COPY'和'ACTION_BUTTON_REFRESH'被稱爲「無法解析符號」 –