简体   繁体   中英

Android widget Intent without layout

is it possible to make an Intent whic runs in background? Like a service i assume.

I need to perform an action (call webservice) when the user clicks on a widget.

My code below

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

Should I make my intent to perform an action to a service or something like that? Thanks!

UPDATE: Im trying to do it by calling an IntentService now, but it doesnt calls it. My code below:

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

any idea?

  • I am not sure what you are trying to achieve
  • I am right you want to call a widget on click of a widget(view)

  • This can be achieved in a simple way by calling the onClick action of a widget and place the web-service invocation code inside the onCllickListener

Sample

button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
              //Place your webservice invocation code here
}}

If you want to use service in some way have a look at android documentation on how to use it

Hope this helps !

I solved it by calling a IntentService.

Here is my code:

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

Thanks @Karakuri

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM