Android小部件在一段时间后停止工作?

前端之家收集整理的这篇文章主要介绍了Android小部件在一段时间后停止工作?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个带小部件的手电筒应用程序.小部件用于打开和关闭手电筒,不显示主要活动或任何内容.然而,几个小时后,小部件什么都不做.我的意思是如果你点击它,没有任何反应.
我有两个类来完成提供者和接收者.

提供者:

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,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分钟后再次运行,然后它可能会在某个时间再次冻结.

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

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

解决方法

请看这篇文章,我认为这个问题在这里解释了 the dark side of app widgets

猜你在找的Android相关文章