2
以下是我的設置。 我有一個livecard服務類,它執行一個異步任務來從外部獲取天氣和weatherforecast數據。 它還啓動了一個Pendingintent,其中兩個菜單項是「ShowForecast」&「Stop」如何從服務呼叫接收異步數據時更新Google Glass菜單
天氣數據在主屏幕上顯示到達時顯示。 但預測數據需要更長的時間。我想隱藏ShowForecast菜單,直到異步任務成功完成。
實現此目的的最佳方法是什麼?我讀過一些關於全局變量的內容,或者通過intent.putextra或直接更新卡片菜單。 我現在想的是在我的活動類中使用一個布爾值,它在onPrepareOptionsMenu中籤入並隱藏/顯示菜單。
但是,如何在異步任務完成時從Service類設置此布爾值? 以下是課堂片段。所有建議歡迎您致電! :)
public class LiveCardMenuActivity extends Activity {
private static final String TAG = LiveCardMenuActivity.class.getSimpleName();
// default disabled menu
private boolean menu_showForecast = false;
@Override
// in this method we hide/ show forecast menu, depending if the service has gotten the data
public boolean onPrepareOptionsMenu(Menu menu) {
if(!menu_showForecast) {
menu.findItem(R.id.action_show_forecast).setVisible(false);
}
return super.onPrepareOptionsMenu(menu);
...
,並與異步任務
public class LiveCardService extends Service {
private static final String TAG = LiveCardService.class.getSimpleName();
private static final String LIVE_CARD_TAG = "LiveCardService";
private LiveCard mLiveCard;
private RemoteViews mLiveCardView;
private final Handler mHandler = new Handler();
private final UpdateLiveCardRunnable mUpdateLiveCardRunnable = new UpdateLiveCardRunnable();
private static final long DELAY_MILLIS = 1000;
// keep the weather info central, due to reuse and forecast cards
private Weather weather = new Weather();
private WeatherForecast weatherForecast = new WeatherForecast();
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
// and get the weather data & icon, async call
String loc = "id=2755420";
JSONWeatherTask task = new JSONWeatherTask();
task.execute(new String[]{loc});
// including the weather forecast
JSONWeatherForecastTask taskForecast = new JSONWeatherForecastTask();
taskForecast.execute(new String[]{loc});
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (mLiveCard == null) {
// Get an instance of a live card
mLiveCard = new LiveCard(this, LIVE_CARD_TAG);
// setup live card views
mLiveCardView = new RemoteViews(getPackageName(), R.layout.live_card);
mLiveCard.setViews(mLiveCardView);
// Display the options menu when the live card is tapped.
Intent menuIntent = new Intent(this, LiveCardMenuActivity.class);
mLiveCard.setAction(PendingIntent.getActivity(this, 0, menuIntent, 0));
mLiveCard.publish(PublishMode.REVEAL);
// Queue the update text runnable
mHandler.post(mUpdateLiveCardRunnable);
} else {
mLiveCard.navigate();
}
return START_STICKY;
}
...
private class JSONWeatherForecastTask extends AsyncTask<String, Void, WeatherForecast> {
@Override
protected WeatherForecast doInBackground(String... params) {
//
String data = ((new WeatherHttpClient()).getWeatherForecastData(params[0]));
try {
weatherForecast = JSONWeatherForecastParser.getWeatherForecast(data);
} catch (JSONException e) {
e.printStackTrace();
}
return weatherForecast;
}
@Override
protected void onPostExecute(WeatherForecast weatherForecast) {
super.onPostExecute(weatherForecast);
// there is no showing of data yet, except voor enabling the forecast menu
Weather[] weatherForecastArray = weatherForecast.getWeatherForecastArray();
int count = weatherForecastArray.length;
if(count > 0){
//mLiveCard menu update or boolean update?
}
}
}
嗨阿蘭,THX的幫助。當服務類是我的設置中的主類時,使用綁定進行此設置是否也可以工作?這似乎發生在一個班,這使得它更容易一些。或者我錯過了某處的整體畫面。 :( – Jinxvar 2015-01-22 19:24:25
不再需要,用你的答案解決了它,我在服務類上創建了一個活頁夾,並從活動中綁定它,從準備菜單時我稱之爲getter的活動中,getter檢查預測是否已經收到作品喜歡的魅力,thx了! – Jinxvar 2015-01-22 20:56:09