2017-03-03 30 views
0

初めてウィジェットの開発を行っています。だから私と一緒に耐えてください。私はonStart()からウィジェットを起動するAndroidサービスを持っています。だから私は自分のWidgetProviderクラスonUpdate()にいて、ボタンのようなビューを更新します。 私の質問は -Androidホーム画面ウィジェット

1)どのように私はウィジェットのボタンのプレスを処理し、サービスがその仕事をするようにボタンプレスについてサービスに伝えますか?

2)サービスが終了したら、ウィジェットのボタンを更新する必要があります。どうすればいいですか?

3)アプリが殺されると、サービスも殺されます。だから私がウィジェットをクリックすると、そのアプリが殺されてアプリが開けたと判断する方法はありますか?

4)アプリのUIがサービスと対話していてサービスからの応答を受け取っている場合、どのようにこれらの応答をウィジェットにも伝えますか?

マイウィジェットのコードは、このようなものです -

public class MyWidgetProvider extends AppWidgetProvider { 

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

     RemoteViews remoteViews; 
     ComponentName componentName; 

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

     remoteViews.setOnClickPendingIntent(R.id.button1,getPendingSelfIntent(context, "Play",componentName)); 
     remoteViews.setOnClickPendingIntent(R.id.button2,getPendingSelfIntent(context, "Pause",componentName)); 

     appWidgetManager.updateAppWidget(componentName, remoteViews); 
    } 

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

     if ("Play".equals(intent.getAction())) { 

     //I really dont know what to do here.... 

      AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); 

      RemoteViews remoteViews; 
      ComponentName watchWidget; 

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



      appWidgetManager.updateAppWidget(watchWidget, remoteViews); 

     } 
    } 

    protected PendingIntent getPendingSelfIntent(Context context, String action, ComponentName componentName) { 
     Intent intent = new Intent(context, MyService.class); 
     intent.setAction(action); 
     intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, componentName); 
     return PendingIntent.getService(context, 0, intent, 0); 
    } 
} 

あなたはすでにあなたのgetPendingSelfIntent()方法であることをやっている)この

答えて

1

1で私を助けてくださいすることができ要求します。あなたの目標は、このようなMyWidgetProviderすべきである:

Intent serviceIntent = new Intent(context, MyService.class); 

2)のために:

Intent intent = new Intent(context, MyWidgetProvider.class); 

意図アクションが「再生」し、そこから自分のサービスを開始している場合は、あなたのonReceiveであなたがチェックしますあなたの時間場合は、(あなたが実際に、それを行うことはできません)アプリを開き、明示する必要はありません)

Intent widgetUpdateIntent = new Intent(this, MyWidgetProvider.class); 
widgetUpdateIntent.setAction(ApplicationEvents.DATA_FETCHED);//this is your custom action 
widgetUpdateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); 
sendBroadcast(widgetUpdateIntent); 

3:あなたのサービスでは、ブロードキャストを送信する必要があなたのウィジェットに伝えます更新サイクルを30分に設定すると、onUpdateコールバックを受信し、そこからサービスを開始する必要があります。

4)アクションがカスタムアクション(ApplicationEvents.DATA_FETCHED)と一致すると、onReceiveイベントのUIが更新されます。

+0

ありがとうございました:)第3のポイントで、私は実際に私のアンドロイドを持っています:updatePeriodMillis = "1000" xmlファイルにあります。しかし、Spotifyは、アプリが殺されてウィジェットをクリックすると、明示的にアプリを開きます。 – MagdHo0506

+0

[documentation](https://developer.android.com/reference/android/appwidget/AppWidgetProviderInfo.html#updatePeriodMillis)に記載されている最小値は30分です。低い値を使用するには、AlarmManagerを使用する必要があります。 Spotifyについてウィジェットをクリックしたときは、ウィジェットプロバイダがインテントを処理してスケジュールされたアップデートに関係しないため、別のシナリオです。 –

+0

ええ、Spotifyにも同様のものが必要です。私はスケジュールされた更新が必要ない。これはどうすればいいですか? – MagdHo0506

関連する問題