[英]Android widget Intent without layout
是否可以在后台運行Intent? 我想像一項服務。
當用戶單擊窗口小部件時,我需要執行一個操作(調用Web服務)。
我的代碼如下
Intent intent = new Intent();
intent.putExtra("webServiceId", mWSId);
PendingIntent pending = PendingIntent.getActivity(context, 0,intent, 0);
views.setOnClickPendingIntent(R.id.btnWidgetToggle, pending);
appWidgetManager.updateAppWidget(mAppWidgetId, views);
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
我是否應該對某項服務或類似內容采取行動? 謝謝!
更新:我試圖通過立即調用IntentService來做到這一點,但它沒有調用它。 我的代碼如下:
Intent intent = new Intent(context, widgetService.class);
intent.putExtra("webServiceId",mWSId);
PendingIntent pending = PendingIntent.getService(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.btnWidgetToggle, pending);
任何想法?
widget(view)
onClick
動作並將web-service
調用代碼放在onCllickListener
內的簡單方式來實現 樣品
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Place your webservice invocation code here
}}
如果您想以某種方式使用服務,請查看有關如何使用服務的android文檔
希望這可以幫助 !
我通過調用IntentService解決了它。
這是我的代碼:
Intent intent = new Intent(context, widgetService.class);
intent.putExtra("webServiceId",mWSId);
PendingIntent pending = PendingIntent.getService(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.btnWidgetToggle, pending);
謝謝@Karakuri
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.