2017-09-03 4 views
1

을 나는 간단한 텍스트와 3 개 버튼 표시 위젯이 있습니다위젯 내에서 여러 개의 버튼을 취급 안드로이드

  • 새로 고침을
  • 복사 (의 내용을 복사 (다른 임의의 문자열을 시설하고 표시합니다) 클립 보드에 텍스트 뷰)
  • 공유 (이미 새로 고침 버튼 설정있어 그냥 잘 작동

소셜 미디어 등)에 텍스트 뷰의 컨텐츠를 공유하지만 난 방법을 알아낼 수없는 것 다른 두 개의 버튼을 처리하기 위해

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에 당신의 버튼

설정 출원 의도 : ExampleAppWidget.class (즉의 AppWidgetProvider에서 확장) OnReceive를 오버라이드 (override)에서

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

() 메소드를 다음 동안 내가 잃었어요

@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'은 "심볼을 해결할 수 없습니다" –