{"btc_usd":{"high":880,"low":836,"avg":858,"vol":3774491.17766,"vol_cur":4368.01172,"last":871.999,"buy":872,"sell":870.701,"updated":1482754417}}
<source lang="xml"><?xml version="1.0" encoding="utf-8"?> <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:initialKeyguardLayout="@layout/btcwidget" android:initialLayout="@layout/btcwidget" android:minHeight="40dp" android:minWidth="110dp" android:previewImage="@drawable/example_appwidget_preview" android:resizeMode="horizontal|vertical" android:updatePeriodMillis="180000" android:widgetCategory="home_screen"></appwidget-provider>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#09C" android:padding="@dimen/widget_margin"> <TextView android:id="@+id/appwidget_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_margin="8dp" android:background="#09C" android:contentDescription="@string/appwidget_text" android:text="@string/appwidget_text" android:textColor="#ffffff" android:textSize="20sp" android:textStyle="bold|italic" /> </RelativeLayout>
/** * Implementation of App Widget functionality. */ public class BTCWidget extends AppWidgetProvider { static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) { CharSequence widgetText = context.getString(R.string.appwidget_text); // Construct the RemoteViews object RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.btcwidget); views.setTextViewText(R.id.appwidget_text, widgetText); // Instruct the widget manager to update the widget appWidgetManager.updateAppWidget(appWidgetId, views); } @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { // There may be multiple widgets active, so update all of them for (int appWidgetId : appWidgetIds) { updateAppWidget(context, appWidgetManager, appWidgetId); } } @Override public void onEnabled(Context context) { // Enter relevant functionality for when the first widget is created } @Override public void onDisabled(Context context) { // Enter relevant functionality for when the last widget is disabled } }
/** * Implementation of App Widget functionality. */ public class BTCwidget extends AppWidgetProvider { private static final String SYNC_CLICKED = "btcwidget_update_action"; private static final String WAITING_MESSAGE = "Wait for BTC price"; public static final int httpsDelayMs = 300; // , static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) { // RemoteViews : RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.btcwidget); // - TextView views.setTextViewText(R.id.appwidget_text, WAITING_MESSAGE); appWidgetManager.updateAppWidget(appWidgetId, views); String output; // // - HTTPRequestThread thread = new HTTPRequestThread(); thread.start(); try { while (true) { Thread.sleep(300); if(!thread.isAlive()) { output = thread.getInfoString(); break; } } } catch (Exception e) { output = e.toString(); } // views.setTextViewText(R.id.appwidget_text, output); // Instruct the widget manager to update the widget appWidgetManager.updateAppWidget(appWidgetId, views); } @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { RemoteViews remoteViews; ComponentName watchWidget; remoteViews = new RemoteViews(context.getPackageName(), R.layout.btcwidget); watchWidget = new ComponentName(context, BTCwidget.class); // , remoteViews.setOnClickPendingIntent(R.id.appwidget_text, getPendingSelfIntent(context, SYNC_CLICKED)); appWidgetManager.updateAppWidget(watchWidget, remoteViews); // for (int appWidgetId : appWidgetIds) { updateAppWidget(context, appWidgetManager, appWidgetId); } } // , // @Override public void onReceive(Context context, Intent intent) { super.onReceive(context, intent); if (SYNC_CLICKED.equals(intent.getAction())) { AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); RemoteViews remoteViews; ComponentName watchWidget; remoteViews = new RemoteViews(context.getPackageName(), R.layout.btcwidget); watchWidget = new ComponentName(context, BTCwidget.class); remoteViews.setTextViewText(R.id.appwidget_text, WAITING_MESSAGE); //updating widget appWidgetManager.updateAppWidget(watchWidget, remoteViews); String output; HTTPRequestThread thread = new HTTPRequestThread(); thread.start(); try { while (true) { Thread.sleep(httpsDelayMs); if(!thread.isAlive()) { output = thread.getInfoString(); break; } } } catch (Exception e) { output = e.toString(); } remoteViews.setTextViewText(R.id.appwidget_text, output); //widget manager to update the widget appWidgetManager.updateAppWidget(watchWidget, remoteViews); } } // protected PendingIntent getPendingSelfIntent(Context context, String action) { Intent intent = new Intent(context, getClass()); intent.setAction(action); return PendingIntent.getBroadcast(context, 0, intent, 0); } }
package com.hakey.btcwidget; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.Calendar; class HTTPRequestThread extends Thread{ private static final String urlString = "https://btc-e.nz/api/3/ticker/btc_usd"; String getInfoString() { return output; } private String output = ""; private void requestPrice() { try { URL url = new URL(urlString); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("GET"); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); output = "Price: " + JSONParser.getPrice(response.toString()) + "\n" + getTimeStamp(); } catch (Exception e) { output = e.toString(); } } @Override public void run() { requestPrice(); } private String getTimeStamp() { Calendar calendar = Calendar.getInstance(); if(calendar.get(Calendar.MINUTE)>9) { return "Time: " + calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE); } else { return "Time: " + calendar.get(Calendar.HOUR_OF_DAY) + ":0" + calendar.get(Calendar.MINUTE); } } }
package com.hakey.btcwidget; import org.json.JSONException; import org.json.JSONObject; class JSONParser { static String getPrice(String s) throws JSONException { String price; JSONObject obj = new JSONObject(s); JSONObject pairObj = obj.getJSONObject("btc_usd"); price = pairObj.getString("last"); return price; } }
Source: https://habr.com/ru/post/318482/
All Articles