gpt4 book ai didi

android - 手电筒小部件无法关闭

转载 作者:行者123 更新时间:2023-11-29 21:15:17 25 4
gpt4 key购买 nike

我已经创建了我的小部件存在和运行所需的一切。即便如此,在第一次点击时,t 还是按照预期的方式进行操作,但随后图像发生了变化并显示有问题,并且无法正常工作。我想让它打开闪光灯然后关闭它。帮助将不胜感激。

FlashlightWidgetProvider

public class FlashlightWidgetProvider 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.flash_widget);
views.setOnClickPendingIntent(R.id.button, pendingIntent);

appWidgetManager.updateAppWidget(appWidgetIds, views);

}
}

FlashlightWidgetReceiver

public class FlashlightWidgetReceiver extends BroadcastReceiver {
private static boolean isLightOn = false;
private static Camera camera;

@Override
public void onReceive(Context context, Intent intent) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.flash_widget);

if(isLightOn) {
views.setImageViewResource(R.id.button, R.drawable.off);
} else {
views.setImageViewResource(R.id.button, R.drawable.on);
}

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
appWidgetManager.updateAppWidget(new ComponentName(context, FlashlightWidgetProvider.class),
views);

if (isLightOn) {
if (camera != null) {
camera.stopPreview();
camera.release();
camera = null;
isLightOn = false;
}

} else {
// Open the default i.e. the first rear facing camera.
camera = Camera.open();

if(camera == null) {
Toast.makeText(context, R.string.no_camera, Toast.LENGTH_SHORT).show();
} else {
// Set the torch flash mode
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, R.string.no_flash, Toast.LENGTH_SHORT).show();
}
}
}
}
}

最佳答案

确保按钮资源指的是 ImageView 而不是常规 Button。我刚开始在我的布局文件中使用一个按钮来尝试这个,我遇到了同样的问题,小部件基本上会崩溃并从主屏幕上删除。当我在布局文件中将按钮更改为 ImageView 时,代码现在可以工作了。

我确实对您的代码进行了一些修改,以防万一它本身不起作用,这里是更新后的 FlashlightWidgetProvider:

public class FlashlightWidgetProvider extends AppWidgetProvider {

@Override
public void onReceive(Context context, Intent intent) {
if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(intent.getAction())) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, getClass()));

Intent broadcastIntent = new Intent(context, FlashlightWidgetReceiver.class);
broadcastIntent.setAction("COM_FLASHLIGHT");
broadcastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0,
broadcastIntent,
PendingIntent.FLAG_UPDATE_CURRENT);

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.flashlight);
views.setOnClickPendingIntent(R.id.flashButton, pendingIntent);

appWidgetManager.updateAppWidget(appWidgetIds, views);
}

super.onReceive(context, intent);
}
}

此外,请确保在 list 中正确注册小部件提供者和接收者(当然,将相关部分替换为您自己的部分):

    <receiver
android:name="com.example.stackoverflowtester.widget.FlashlightWidgetProvider"
android:label="Flashlight" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>

<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/flashlight_widget_provider" />
</receiver>
<receiver android:name="com.example.stackoverflowtester.widget.FlashlightWidgetReceiver" >
<intent-filter>
<action android:name="COM_FLASHLIGHT" />
</intent-filter>
</receiver>

关于android - 手电筒小部件无法关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21579848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com