{"id":297,"date":"2020-03-25T16:48:46","date_gmt":"2020-03-25T08:48:46","guid":{"rendered":"https:\/\/badgameshow.com\/fly\/?p=297"},"modified":"2020-05-19T13:58:15","modified_gmt":"2020-05-19T05:58:15","slug":"android-widget%e6%a1%8c%e9%9d%a2%e5%b0%8f%e7%89%a9%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/fly\/android-widget%e6%a1%8c%e9%9d%a2%e5%b0%8f%e7%89%a9%e4%bb%b6\/","title":{"rendered":"Android Widget\u684c\u9762\u5c0f\u7269\u4ef6"},"content":{"rendered":"<h3>1.\u5148\u5b9a\u7fa9\u60f3\u8981\u986f\u793a\u4f48\u5c40\u7684layout<\/h3>\n<h4>\u4f7f\u7528ConstrainLayout\u7684\u8a71\u5c31\u6c92\u8fa6\u6cd5\u986f\u793a<\/h4>\n<pre><code class=\"language-XML line-numbers\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n&lt;RelativeLayout\n   xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n   xmlns:app=\"http:\/\/schemas.android.com\/apk\/res-auto\"\n   xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\n   android:layout_width=\"match_parent\"\n   android:layout_height=\"match_parent\"&gt;\n  &lt;TextView\n        android:id=\"@+id\/tv_time\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:layout_alignParentStart=\"true\"\n        android:layout_alignParentLeft=\"true\"\n        android:layout_alignParentTop=\"true\"\n        android:layout_marginStart=\"22dp\"\n        android:layout_marginLeft=\"22dp\"\n        android:layout_marginTop=\"13dp\"\n        android:text=\"\u6642\u9593\u986f\u793a\"\n        android:textColor=\"#00ff00\"\n        android:textAppearance=\"@style\/TextAppearance.AppCompat.Large\"\n        app:layout_constraintStart_toStartOf=\"parent\"\n        app:layout_constraintTop_toTopOf=\"parent\" \/&gt;\n&lt;\/RelativeLayout&gt;\n<\/code><\/pre>\n<h3>2.\u5728res\u4e0b\u65b0\u589exml\u7684\u6587\u4ef6\u4e26\u5275\u5efa\u4e00\u500bxml\u6a94\u4f86\u914d\u7f6e\u7269\u4ef6\u5927\u5c0f\u4ee5\u53ca\u521d\u59cb\u5316layout<\/h3>\n<pre><code class=\"language-XML line-numbers\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n&lt;appwidget-provider xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n   android:minWidth=\"300dp\"\n   android:minHeight=\"50dp\"\n   android:initialLayout=\"@layout\/widget\"&gt;\n&lt;\/appwidget-provider&gt;\n<\/code><\/pre>\n<h3>3.\u5beb\u4e00\u500b\u985e\u7e7c\u627fAppWidgetProvider,\u5beb\u5b8cService\u5f8c\u4f86\u9019\u500b\u985e\u8abf\u7528<\/h3>\n<pre><code class=\"language-Java line-numbers\">public class WidgetProvider extends AppWidgetProvider {\n  public static final String TAG = WidgetProvider.class.getSimpleName();\n\n  @Override\n  public void onDeleted(Context context, int[] appWidgetIds) {\n     super.onDeleted(context, appWidgetIds);\n        \/\/\u87a2\u5e55\u4e0a\u5de5\u5177\u88ab\u79fb\u9664\n        Log.d(TAG, \"onDeleted: \");\n        context.stopService(new Intent(context, TimerService.class));\n }\n\n @Override\n public void onDisabled(Context context) {\n        super.onDisabled(context);\n        \/\/\u87a2\u5e55\u4e0a\u6700\u5f8c\u4e00\u500b\u5de5\u5177\u88ab\u79fb\u9664\n        context.stopService(new Intent(context, TimerService.class));\n        Log.d(TAG, \"onDisabled: \");\n  }\n\n @Override\n  public void onEnabled(Context context) {\n     super.onEnabled(context);\n        \/\/\u5de5\u5177\u6dfb\u52a0\u5230\u87a2\u5e55\u4e0a\n       context.startService(new Intent(context, TimerService.class));\n       Log.d(TAG, \"onEnabled: \");\n }\n\n @Override\n public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {\n      super.onUpdate(context, appWidgetManager, appWidgetIds);\n     \/\/\u5de5\u5177\u5237\u65b0\u7684\u6642\u5019\n     \/\/remoteView&amp;AppWidgetManager\n     context.startService(new Intent(context, TimerService.class));\n  }\n}\n<\/code><\/pre>\n<h3>4.\u64b0\u5beb\u60f3\u8981\u8b93\u9019\u500b\u7269\u4ef6\u53d6\u5f97\u4ec0\u9ebc\u8cc7\u8a0a,\u57fa\u672c\u4e0a\u90fd\u8981\u5beb\u5728Service\u88e1\u9762<\/h3>\n<pre><code class=\"language-Java line-numbers\">public class TimerService extends Service {\n    private Timer mTimer;\n    private SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(\"yyyy-MM-dd hh:mm:ss\");\n\n @Nullable\n @Override\n public IBinder onBind(Intent intent) {\n     return null;\n }\n\n @Override\n public void onCreate() {\n        super.onCreate();\n        mTimer = new Timer();\n       mTimer.schedule(new TimerTask() {\n           @Override\n           public void run() {\n               updateView();\n           }\n      }, 0, 1000);\n    }\n\n private void updateView() {\n       String time = mSimpleDateFormat.format(new Date());\n       RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.widget);\n       remoteViews.setTextViewText(R.id.tv_time, time);\n       AppWidgetManager manager = AppWidgetManager.getInstance(getApplicationContext());\n       ComponentName cn = new ComponentName(getApplication(), WidgetProvider.class);\n       manager.updateAppWidget(cn, remoteViews);\n  }\n\n @Override\n public void onDestroy() {\n     super.onDestroy();\n     mTimer = null;\n  }\n}\n<\/code><\/pre>\n<h3>5.\u5728Manifests\u88e1\u914d\u7f6e\u7269\u4ef6\u4ee5\u53caService<\/h3>\n<pre><code class=\"language-XML line-numbers\">&lt;service android:name=\".TimerService\" \/&gt;\n&lt;receiver android:name=\".WidgetProvider\"&gt;\n   &lt;intent-filter&gt;\n     &lt;action android:name=\"android.appwidget.action.APPWIDGET_UPDATE\"\/&gt;\n   &lt;\/intent-filter&gt;\n   &lt;meta-data android:name=\"android.appwidget.provider\"\n         android:resource=\"@xml\/widgetconfig\"\/&gt;\n&lt;\/receiver&gt;\n<\/code><\/pre>\n\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>1.\u5148\u5b9a\u7fa9\u60f3\u8981\u986f\u793a\u4f48\u5c40\u7684layout \u4f7f\u7528ConstrainLayout\u7684\u8a71\u5c31\u6c92\u8fa6\u6cd5\u986f\u793a &lt;?xml v &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[5],"tags":[13,14,32],"class_list":["post-297","post","type-post","status-publish","format-standard","hentry","category-android","tag-android","tag-java","tag-widget"],"_links":{"self":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/297","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/comments?post=297"}],"version-history":[{"count":3,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/297\/revisions"}],"predecessor-version":[{"id":474,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/297\/revisions\/474"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/media?parent=297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/categories?post=297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/tags?post=297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}