2017-03-03 110 views
0

我正在做widget開發的第一次。所以請耐心等待。我有一個Android服務,從onStart()開始部件。所以我在我的WidgetProvideronUpdate()和更新像按鈕的意見。 現在我的問題是 -Android主屏幕小部件

1)我如何處理小工具按鈕的按下,並告訴服務有關按鈕按下,以便服務完成其工作?

2)服務完成後,它需要更新小部件按鈕,我該如何處理?

3)當應用程序被殺害時,服務也被殺死。所以當我點擊小部件時,如何確定該應用程序已被殺死並打開應用程序?

4)如果應用程序中的用戶界面正在與服務對話並接收服務響應,我該如何將這些響應告訴小部件?

我的小部件代碼是這樣的 -

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

請你,請幫我在這

回答

1

1)你已經這樣做,以你的getPendingSelfIntent()方法。雖然你的目標應該是MyWidgetProvider這樣的:

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

然後在你的onReceive你會檢查是否意圖動作是從那裏「播放」,並啓動服務:

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

2)爲了您的服務傳達給你的widget,你需要發送一個廣播:

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)你並不需要顯式打開應用程序(你不能這樣做,實際上),如果你^ h如果您的更新週期設置爲30分鐘,您將收到onUpdate回撥,並且您也需要從那裏啓動您的服務。

4)當動作與您的自定義動作(ApplicationEvents.DATA_FETCHED)匹配時,您將更新onReceive事件中的用戶界面。

+0

謝謝你的回答:)在第三點,我實際上有我的android:updatePeriodMillis =「1000」 在xml文件。但我見過Spotify,其中當應用程序被殺害,並且您點擊小部件時,他們明確地打開該應用程序。 – MagdHo0506

+0

請注意,最小值爲30分鐘,如[文檔](https://developer.android.com/reference/android/appwidget/AppWidgetProviderInfo.html#updatePeriodMillis)所述。爲了使用較低的值,您需要使用AlarmManager。關於Spotify,當您點擊小部件是一種不同的情況,因爲小部件提供程序將處理意圖,因此它與預定更新無關。 –

+0

是的,我也需要類似Spotify。我不需要預定的更新。我該怎麼做呢? – MagdHo0506