How to launch a website by clicking a widget?

I have a basic widget that displays a text box. I am trying to figure out how to launch a website URL when a user clicks on the widget itself. I can not find any code on this.

+3
source share
5 answers

We can not use onClickListenerdirectly with the view in widgets. Instead, we should wrap ours Intentwith PendingIntentand setOnClickPendingIntentto view.

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        // There maybe > 1 instance of our widget
        for (int i : appWidgetIds) {
        startBrowsing(context, appWidgetManager, i);
        }
    }

// Processing click on widget
    private void startBrowsing(Context ctx,
            AppWidgetManager appWidgetManager, int widgetID) {
        RemoteViews widgetView = new RemoteViews(ctx.getPackageName(), R.layout.widget);
        Uri uri = Uri.parse("http://www.google.com");
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        PendingIntent pIntent = PendingIntent.getActivity(ctx, widgetID, intent, 0);
           // viewID - our clickable view ID
        widgetView.setOnClickPendingIntent(R.id.viewID, pIntent);

        appWidgetManager.updateAppWidget(widgetID, widgetView);


    }

Set the update period in the metadata.xml file to 0, because we update it manually

android:updatePeriodMillis="0"

And don't forget

<uses-permission android:name="android.permission.INTERNET"/>
+3
source

First configure the action identifier.

private final String WIDGET_CLICK = "WidgetClick";

PendingIntent . "YourAppWidgetProvider" , AppWidgetProvider.

Intent intent = new Intent(context, YourAppWidgetProvider.class);
intent.setAction(WIDGET_CLICK);
views.setOnClickPendingIntent(R.id.widget_layout, PendingIntent.getBroadcast(context, 0, intent, 0));

, onReceive , . "urlString" http:// .

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

    if (intent.getAction() != null && intent.getAction().equals(WIDGET_CLICK)) {
        try {
            Intent webIntent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse(urlString));
            webIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(webIntent);
        } catch (RuntimeException e) {
            // The url is invalid, maybe missing http://
            e.printStackTrace();
        }
    }
}
+3

2 :

  • - TextView, , , ( )
  • onClickListener - .

:

TextView tv=(TextView ) findViewById(R.id.myTextView);
tv.setText(Html.fromHtml("<a href='stackoverflow.com'>Go StackOverFlow!</a>"));
0

, "", . - , , API RemoteViews, (http://stackoverflow.com/questions/2082998/how-to-implement-a-button- on-an-android-widget), , textview ... .

... , PendingIntent, @CommonsWare .

0

, TextView TextView.setOnClickListener(this), OnClickListener public void onClick(View v).

Inside the method, onClick(View v)do the following:

public void onClick(View v){
 startActivity(new Intent(Intent.ACTION_VIEW, url));
}

This will open urlin the device’s browser.

You may also have another action that WebView uses to render the website.

I hope this was helpful.

-1
source

Source: https://habr.com/ru/post/1786275/


All Articles