<tfoot id='ukDrz'></tfoot>

      <i id='ukDrz'><tr id='ukDrz'><dt id='ukDrz'><q id='ukDrz'><span id='ukDrz'><b id='ukDrz'><form id='ukDrz'><ins id='ukDrz'></ins><ul id='ukDrz'></ul><sub id='ukDrz'></sub></form><legend id='ukDrz'></legend><bdo id='ukDrz'><pre id='ukDrz'><center id='ukDrz'></center></pre></bdo></b><th id='ukDrz'></th></span></q></dt></tr></i><div id='ukDrz'><tfoot id='ukDrz'></tfoot><dl id='ukDrz'><fieldset id='ukDrz'></fieldset></dl></div>

      <small id='ukDrz'></small><noframes id='ukDrz'>

        <bdo id='ukDrz'></bdo><ul id='ukDrz'></ul>
    1. <legend id='ukDrz'><style id='ukDrz'><dir id='ukDrz'><q id='ukDrz'></q></dir></style></legend>
    2. Android小部件在一段时间后停止工作?

      Android widget stops working after a while?(Android小部件在一段时间后停止工作?)

        <tfoot id='pQDtk'></tfoot>
          <legend id='pQDtk'><style id='pQDtk'><dir id='pQDtk'><q id='pQDtk'></q></dir></style></legend>
            • <bdo id='pQDtk'></bdo><ul id='pQDtk'></ul>
                <tbody id='pQDtk'></tbody>

                <i id='pQDtk'><tr id='pQDtk'><dt id='pQDtk'><q id='pQDtk'><span id='pQDtk'><b id='pQDtk'><form id='pQDtk'><ins id='pQDtk'></ins><ul id='pQDtk'></ul><sub id='pQDtk'></sub></form><legend id='pQDtk'></legend><bdo id='pQDtk'><pre id='pQDtk'><center id='pQDtk'></center></pre></bdo></b><th id='pQDtk'></th></span></q></dt></tr></i><div id='pQDtk'><tfoot id='pQDtk'></tfoot><dl id='pQDtk'><fieldset id='pQDtk'></fieldset></dl></div>
              1. <small id='pQDtk'></small><noframes id='pQDtk'>

              2. 本文介绍了Android小部件在一段时间后停止工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个带有小部件的手电筒应用.该小部件用于打开和关闭手电筒,不显示主要活动或任何内容.然而,几个小时后,小部件什么也不做.我的意思是如果你点击它,什么都不会发生.我有两个 来完成这个,一个 Provider 和一个 Receiver.

                I have a flashlight app with a widget. The widget is used to turn the flashlight on and off and does not display main activity or anything. After a few hours, however, the widget does nothing. I mean if you click it, nothing happens. I have two classes to accomplish this a Provider and a Receiver.

                提供者:

                public class WidgetProvider extends AppWidgetProvider {
                
                  @
                  Override
                  public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                    int[] appWidgetIds) {
                
                    Intent receiver = new Intent(context, FlashlightWidgetReceiver.class);
                    receiver.setAction("COM_FLASHLIGHT");
                    receiver.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
                    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, receiver, 0);
                
                    RemoteViews views = new RemoteViews(context.getPackageName(),
                      R.layout.appwidget_layout);
                    views.setOnClickPendingIntent(R.id.imageButton, pendingIntent);
                
                    appWidgetManager.updateAppWidget(appWidgetIds, views);
                
                  }
                }

                接收者:

                public class FlashlightWidgetReceiver extends BroadcastReceiver {
                  private static boolean isLightOn = false;
                  private static Camera camera;
                  MediaPlayer mp;@
                  Override
                  public void onReceive(Context context, Intent intent) {
                    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_layout);
                
                    if (isLightOn) {
                      views.setImageViewResource(R.id.imageButton, R.drawable.btn_switch_off);
                      mp = MediaPlayer.create(context, R.raw.light_switch_off);
                    } else {
                      views.setImageViewResource(R.id.imageButton, R.drawable.btn_switch_on);
                      mp = MediaPlayer.create(context, R.raw.light_switch_on);
                    }
                    mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                
                      @
                      Override
                      public void onCompletion(MediaPlayer mp) {
                        // TODO Auto-generated method stub
                        mp.release();
                      }
                    });
                    mp.start();
                    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
                    appWidgetManager.updateAppWidget(new ComponentName(context, WidgetProvider.class),
                      views);
                
                    if (isLightOn) {
                      if (camera != null) {
                        camera.stopPreview();
                        camera.release();
                        camera = null;
                        isLightOn = false;
                      }
                
                    } else {
                      camera = Camera.open();
                
                      if (camera == null) {
                        Toast.makeText(context, "No Camera!", Toast.LENGTH_SHORT).show();
                      } else {
                        Camera.Parameters param = camera.getParameters();
                        param.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                        try {
                          camera.setParameters(param);
                          camera.startPreview();
                          isLightOn = true;
                        } catch (Exception e) {
                          Toast.makeText(context, "No Flash!", Toast.LENGTH_SHORT).show();
                        }
                      }
                    }
                  }
                }

                设置:

                <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
                    android:minWidth="40dp"
                    android:minHeight="40dp"
                    android:maxWidth="40dp"
                    android:maxHeight="40dp"
                    android:updatePeriodMillis="86400000"
                    android:initialLayout="@layout/appwidget_layout"
                    android:resizeMode="horizontal|vertical">
                </appwidget-provider>

                更新:减少更新间隔会使小部件更频繁地刷新,因此如果它被卡住,它会在 30 分钟后再次工作,然后它可能会在某个时候再次冻结.

                Update: Reducing the update interval makes the widget refresh more often so if it's stuck it works again after 30 min and then it might freeze again sometime.

                更新 2:更改日期会立即冻结小部件,直到它被刷新.

                Update 2: Changing the date instantly freezes the widget until it's refreshed.

                更新 3:更改日期会以某种方式重新启动启动器,并且每当启动器重新启动时,小部件都会冻结 30 分钟.

                Update 3: Changing the date somehow restarts the launcher and whenever the launcher is restarted the widget freezes for 30 mins.

                推荐答案

                好吧,我终于有时间一劳永逸地解决这个问题了:)

                Alright guys, I finally got time to fix this problem once and for all :)

                我为提供者创建了更多方法,而不是在 onUpdate 中做所有事情,需要一个重要的方法:

                I created more methods for the provider instead of doing everything in onUpdate, one important method needed:

                    public static PendingIntent buildButtonPendingIntent(Context context) {
                      Intent intent = new Intent();
                      intent.setAction("COM_FLASHLIGHT");
                      return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                    }

                当小部件被点击时,这个方法会通过接收器调用,代码如下:

                And this method is called through the receiver when the widget is clicked using the code below:

                private void turnFlash(Context context) {
                    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_layout);
                    views.setOnClickPendingIntent(R.id.imageButton,       WidgetProvider.buildButtonPendingIntent(context));
                }

                就是这样,不要再打嗝了!

                That is all, no more hiccups!

                这篇关于Android小部件在一段时间后停止工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How can I use CClistview in COCOS2d Android?(如何在 COCOS2d Android 中使用 CClistview?)
                cocos2d-android: how to display score(cocos2d-android:如何显示分数)
                Sqlite database not copied from asset folder Android(Sqlite 数据库未从资产文件夹 Android 复制)
                SQLite Database Copy Appears Corrupted When Generated by Device and not Emulator(SQLite 数据库副本在由设备而不是模拟器生成时出现损坏)
                Android file copy(安卓文件拷贝)
                Android how to detect Copy event of Edittext in android(Android如何在android中检测Edittext的Copy事件)

                      <tbody id='JQwpQ'></tbody>
                      <bdo id='JQwpQ'></bdo><ul id='JQwpQ'></ul>
                      1. <small id='JQwpQ'></small><noframes id='JQwpQ'>

                        <legend id='JQwpQ'><style id='JQwpQ'><dir id='JQwpQ'><q id='JQwpQ'></q></dir></style></legend>
                      2. <tfoot id='JQwpQ'></tfoot>
                        <i id='JQwpQ'><tr id='JQwpQ'><dt id='JQwpQ'><q id='JQwpQ'><span id='JQwpQ'><b id='JQwpQ'><form id='JQwpQ'><ins id='JQwpQ'></ins><ul id='JQwpQ'></ul><sub id='JQwpQ'></sub></form><legend id='JQwpQ'></legend><bdo id='JQwpQ'><pre id='JQwpQ'><center id='JQwpQ'></center></pre></bdo></b><th id='JQwpQ'></th></span></q></dt></tr></i><div id='JQwpQ'><tfoot id='JQwpQ'></tfoot><dl id='JQwpQ'><fieldset id='JQwpQ'></fieldset></dl></div>