开发者

How to launch website on widget click?

I have a basic widget that displays a text field. I'开发者_如何转开发m trying to figure out how to launch a website url when a user clicks on the widget itself. I can't seem to find any code on the matter.


We can't use onClickListener directly with view in widget. Instead we should wrap our Intent with PendingIntent and setOnClickPendingIntent to 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 update period to 0 in metadata.xml because we update it manually

android:updatePeriodMillis="0"

And don't forget

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


First, set up an action identifier.

private final String WIDGET_CLICK = "WidgetClick";

Then, when initializing the widget, register a PendingIntent for the root view of your widget. Replace "YourAppWidgetProvider" with your widget class that overrides AppWidgetProvider.

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

Finally, override onReceive and use the context provided by it to launch your website. Replace "urlString" with the address of your website and don't forget to include http:// in the address.

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


Basically there are 2 options:

  1. Link alike - create linkified TextView, so by clicking on those link you'll be there (in web)
  2. Through onClickListener - described in previous answer.

I'd prefer 1st approach:

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


I'm guessing since you said "widget" that the previous answers aren't working for you. I'm in the same boat--you'll likely need to set a pending intent via the RemoteViews API like is done here (http://stackoverflow.com/questions/2082998/how-to-implement-a-button-on-an-android-widget) except that you'll pass in the textview ID instead of a button ID... I think.

You shouldn't need the fancy intent action stuff... just replace the Intent that they use in the PendingIntent call with the one from @CommonsWare's answer to this question.


set that TextView to be clickable with TextView.setOnClickListener(this) and then let your activity implement the OnClickListener interface and define the public void onClick(View v) method.

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

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

This will open the url in the device browser.

You can also have another activity that uses WebView to display the website.

I hope it was helpful.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜