虽然我很欣赏App Widget不WebView
直接支持,但是它是否可以使用ImageView
(支持),以及这里描述的技术来生成图像ImageView
?该WebView
不会直接使用,但只在后台使用,以用于提供图像ImageView
.
android webview imageview appwidgetprovider android-appwidget
我创建了一个Android小部件.
在运行Android 4.4.4的Genymotion Nexus 4模拟器中,一切运行良好.
在运行Android 4.4.4的Nexus 4设备上,我将小部件放在主屏幕上,然后变成了Google App小部件.
然后它变成我的小部件,然后再次进入Google App小部件,依此类推.它执行此操作直到我从主屏幕中删除小部件.
此外,我使用parse.com作为在线存储,在我的手机上似乎没有获得数据.我无法确切地说出因为小部件不断变化.
我的小部件包含3个文件.
一个扩展应用程序类:
public class MyApp extends Application
{
private MyModel model;
@Override
public void onCreate() {
Parse.enableLocalDatastore(this);
ParseObject.registerSubclass(GdbdData.class);
Parse.initialize(this, "-", "-");
if(model == null) {
Intent intent = new Intent(this,GdbdWidgetProvider.class);
intent.setAction(WidgetUtils.WIDGET_REFRESH_STORAGE);
intent.putExtra("userId",123);
sendBroadcast(intent);
}
super.onCreate();
}
Run Code Online (Sandbox Code Playgroud)
一个WidgetProvider:
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
final String action = intent.getAction();
if (action.equals(WidgetUtils.WIDGET_REFRESH_STORAGE))
{
MyApp app = ((MyApp)context.getApplicationContext());
int userId = intent.getIntExtra("userId",0);
TryGetModelFromRemoteStorage(userId,context);
}
}
static void …
Run Code Online (Sandbox Code Playgroud) 我有一个时钟小部件Android应用程序,我现在正在尝试更新到API 26要求.
到目前为止,我使用了一个后台服务,它在onCreate
方法a中开始注册,BroadcastReceiver
以接收系统广播,例如android.intent.action.SCREEN_ON, android.intent.action.SCREEN_OFF, android.intent.action.TIME_SET, android.intent.action.TIMEZONE_CHANGED
.然后,当屏幕关闭时,此服务暂停时钟,并在屏幕重新打开时将其唤醒以节省电池电量.
在Oreo中,这种服务似乎不是一种选择,因为它必须在前台运行,并且通知对用户来说真的没有意义.另外,据我在文档中看到,JobScheduler
无法帮助我,因为我还没有发现可以在屏幕打开时安排作业.
我试图创建一个BroadcastReceiver
的内AppWidgetProvider
类,并在对其进行注册AppWidgetProvider
的onUpdate
方法来接收所述系统广播.这很好用,广播确实收到了,但直到屏幕保持关闭一段时间; 之后,似乎应用程序以某种方式被系统杀死,或者在没有任何报告错误或崩溃的情况下停止工作; 但是,如果我点击它,它将正常打开配置活动.
我的问题:
如果我不想运行前台服务,如何在API 26+上正确收听屏幕开/关广播?
是否可以从AppWidgetProvider
类本身监听系统广播,通过BroadcastReceiver
在其中注册,或甚至注册AppWidgetProvider
自己接收系统事件(无论如何AppWidgetProvider
是扩展BroadcastReceiver
).
为什么我AppWidgetProvider
会在一段睡眠期后暂时停止接收广播的系统意图?
编辑:
我在Android文档中找到了以下registerReceiver
方法中的以下内容,这似乎是我的问题2和3的答案.
注意:无法从BroadcastReceiver组件调用此方法; 也就是说,来自在应用程序清单中声明的BroadcastReceiver.但是,从另一个在运行时使用registerReceiver(BroadcastReceiver,IntentFilter)注册的BroadcastReceiver调用此方法是可以的,因为这样的已注册BroadcastReceiver的生命周期与注册它的对象相关联.
我会得出结论,我对BroadcastReceiver
内部的使用和注册AppWidgetProvider
与此规范相反.
我将保留这篇文章,因为其他人可能会发现这些信息有用,我的问题1仍然有效.
我知道这已被多次询问,但我从上到下阅读了文档,在这里阅读了所有答案,但没有一个帮助.说实话,每个答案都说明了如何解决这个问题.
现在回到我的问题.我想从某些活动更新小部件列表视图,我WidgetProvider#sendUpdateBroadcastToAllWidgets()
为此目的创建了我从活动中调用的内容.
它最终调用,onUpdate()
以便正确接收广播.但意见没有刷新.
我也尝试调用AppWidgetManager#notifyAppWidgetViewDataChanged()
和刷新数据,WidgetFactory#onDataSetChanged()
但从未调用过该方法.
所以我猜这一切都行不通,因为远程视图工厂被缓存但我不知道如何可靠地克服这一点.有什么想法吗?
那些背景呢?我总是要提供一个,但我真的不在乎哪一个.有关系吗?
谢谢
提供商
public class WidgetProvider extends AppWidgetProvider {
public static void sendUpdateBroadcastToAllWidgets(Context context) {
int allWidgetIds[] = AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, WidgetProvider.class));
Intent intent = new Intent(context, WidgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
context.sendBroadcast(intent);
}
@Override
public void onUpdate(Context context, AppWidgetManager widgetManager, int[] widgetIds) {
for (int id : widgetIds) {
updateWidget(context, widgetManager, id);
}
super.onUpdate(context, widgetManager, widgetIds);
}
@Override
public void onDeleted(Context context, int[] widgetIds) {
WidgetPreferences prefs = …
Run Code Online (Sandbox Code Playgroud) 我一直在研究一个小部件并取得一些进展(我希望),但仍然无法让它做我想做的事情.
我有一个配置器活动,可以正常工作.当它关闭时,我调用WordWidget类中的updateAppWidget方法,小部件更新随机数.
由于onClick,我无法做到的随机数变化.根据日志我输入onReceive()方法,但那就是它.
在日志中我打印小部件ID.我注意到,当从配置器活动运行时,它打印appWidgetId = 33,但是当我按下小部件时,它会打印appWidgetId = 24.
这是代码:
public class WordWidget extends AppWidgetProvider
{
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
//This is run when a new widget is added or when the update period expires.
Log.v("wotd", "In onUpdate(), updating " + appWidgetIds.length + " widgets");
Log.v("wotd", " the array is " + appWidgetIds.toString());
for(int x = 0; x < appWidgetIds.length; x++)
{
Integer appWidgetId = appWidgetIds[x];
//Method that updates the random value
updateAppWidget(context, appWidgetManager, appWidgetId); …
Run Code Online (Sandbox Code Playgroud) 我有一个Android应用程序,我已经在棒棒糖之前为它做了一个appwidget,由于某些原因,小部件没有出现在棒棒糖中.但是,它出现在前棒棒糖设备中.
这是我的代码:
AndroidManifest.xml中
<receiver android:name=".widgets.NewsWidgetProvider"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >>
<intent-filter >
<action
android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/news_info" />
</receiver>
Run Code Online (Sandbox Code Playgroud)
news_info.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget_news"
android:minHeight="72dp"
android:minWidth="300dp"
android:updatePeriodMillis="6000000" >
Run Code Online (Sandbox Code Playgroud)
NewsWidgetProvider.java
public class NewsWidgetProvider extends AppWidgetProvider {
private static final String NEXT_NEWS = "NEXT_NEWS";
private static final String PREVIOUS_NEWS = "PREVIOUS_NEWS";
private static final String GO_TO_NEWS_ACTIVITY = "GO_TO_NEWS_ACTIVITY";
private final String NEWS_LIST_KEY = "newsList";
NewsItem[] news;
int currentNews=0;
Bitmap imageBitmap;
private final String CURRENT_NEWS_KEY = "currentNews";
@Override
public void onUpdate(final …
Run Code Online (Sandbox Code Playgroud) android appwidgetprovider android-appwidget android-5.0-lollipop
所以我有一个很好的刷新的app小部件,将一个新的位图加载到一个ImageView
常规的发条中.然后,在某些时候,默默地,它会停止更新.我可以从日志和我的代码正在查询的服务器上的活动中看出,小部件实际上仍在继续定期触发(通过播放的广播onReceive()
,并按预期执行其操作.唯一没有发生的事情是小部件内容没有更新.
因此,在小部件触发时执行的代码中,我正在创建一个新的RemoteView并向其添加内容,包括通过加载新的位图remoteViews.setImageViewBitmap(imageViewID, bmp)
,然后最终调用appWidgetManager.updateAppWidget(appWidgetId, remoteViews)
以更新小部件内容.很好地工作......直到它停止工作.
我不确定要发布什么代码,因为99%的情况下这个东西工作得很好,但偶尔updateAppWidget()
停止工作,没有明显的原因.似乎这是一个系统问题,而不是我的代码中的错误,但我可能是错的.
唯一似乎可能提示这一点的是当连接发生变化时,例如从无信号到WiFi,可能经常发生这种变化.在这些情况下似乎发生了更多.
一旦小部件冻结,唯一解冻它的是重启设备.如果不重新启动,即使删除小部件并添加新小部件也不起作用.实际上,添加一个新的小部件会导致死小部件甚至不响应单击以打开Config Activity,大概是因为remoteViews.setOnClickPendingIntent(R.id.widget, configPendingIntent)
被忽略了,就像RemoteViews
我仔细构造和发送到的其他内容一样appWidgetManager.updateAppWidget()
.
它似乎与其他许多人描述的一样:
https://code.google.com/p/android/issues/detail?id=28216
到目前为止似乎没有已知的治疗方法.https://code.google.com/p/android/issues/detail?id=28216#c56上的帖子似乎提供了一些承诺,但我尝试过AppWidgetHost.startListening()
在各个地方打电话,包括定期通过重复闹钟看看我是否可以启动小部件,但似乎没有任何工作.
其他用户(而不是开发人员)似乎也看到了这个问题:http://androidforums.com/threads/clock-widget-keeps-freezing-please-help.530333/
有任何想法吗?它正在慢慢地让我发疯!
android google-api remoteview appwidgetprovider android-appwidget
我在stackoverflow上读过几个关于这个的问题,但没有一个答案能解决我的问题.
我正在尝试向我的应用添加主屏幕小部件,我的小部件不会出现在Android的小部件列表中.
我尝试重新启动设备,重新安装应用程序,更改配置文件中的参数,确保SD卡上没有安装应用程序.什么都行不通.
这是我目前的代码:
在AndroidManifest.xml的应用程序标记内:
<receiver
android:name=".GulpWidgetProvider"
android:icon="@mipmap/ic_launcher"
android:label="Gulp">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/gulp_widget_info" />
</receiver>
Run Code Online (Sandbox Code Playgroud)
在/res/xml/gulp_widget_info.xml里面:
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/layout_widget"
android:minHeight="150dip"
android:minWidth="400dip"
android:updatePeriodMillis="86400000"
android:widgetCategory="home_screen"
android:resizeMode="none"
android:minResizeHeight="150dip"/>
Run Code Online (Sandbox Code Playgroud)
在/res/layout/layout_widget.xml里面:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="150dip"
android:background="@drawable/gradient"
android:gravity="center_horizontal|top">
<RelativeLayout
android:layout_width="170dip"
android:layout_height="150dip"
android:gravity="center">
<ProgressBar
android:id="@+id/widget_consumption_progress_bar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="150dip"
android:layout_height="150dip"
android:layout_centerInParent="true"
android:indeterminate="false"
android:max="100"
android:progress="0"
android:progressDrawable="@drawable/progressbar"
android:secondaryProgress="100" />
<ProgressBar
android:id="@+id/widget_time_progress_bar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="150dip"
android:layout_height="150dip"
android:layout_centerInParent="true"
android:indeterminate="false"
android:max="100"
android:progress="0"
android:progressDrawable="@drawable/progressbar2"
android:secondaryProgress="100" />
<TextView
android:id="@+id/widget_water_consumed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:fontFamily="sans-serif-light"
android:gravity="center"
android:maxLines="1"
android:textColor="@color/white"
android:textSize="30sp" />
<TextView
android:id="@+id/widget_unit"
android:layout_width="wrap_content"
android:layout_height="wrap_content" …
Run Code Online (Sandbox Code Playgroud) 我有一个带小部件的手电筒应用程序.小部件用于打开和关闭手电筒,不显示主要活动或任何内容.然而,几个小时后,小部件什么都不做.我的意思是如果你点击它,没有任何反应.我有两个课来完成这个a Provider
和a Receiver
.
Provider:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
Receiver:
public class FlashlightWidgetReceiver extends BroadcastReceiver {
private static boolean isLightOn = false;
private static Camera camera;
MediaPlayer mp;@
Override
public void onReceive(Context context, Intent intent) { …
Run Code Online (Sandbox Code Playgroud)我有一个小部件应用程序,没有问题Android Lollipop或较低的操作系统.当我将nexus 5升级到Marshmallow(android 6.0)时,我意识到我的小部件在加载自定义行列表视图时遇到问题.Widget有一个动态textView和一个动态listView.textView工作得很好; 但是listView行项目没有希望.当我添加小部件时,行处于加载阶段.它与Lollipop或更低版本的设备完全相同,但不适用于Marshmallow.我相信棉花糖正在引发一个我无法找到它的问题.我希望有人告诉我我做错了什么并帮助我解决这个问题.我很感激所有的帮助.谢谢.
WidgetProvider.class:
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
System.out.println("WidgetProvider.onUpdate()");
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
final boolean isMaviKart = MainUtils.getCardType(context).equals(Constants.CARD_TYPE_MAVI);
try {
//set widget id
ComponentName thisWidget = new ComponentName(context, WidgetProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
MainUtils.setWidgetID(prefs, allWidgetIds[allWidgetIds.length - 1]);
for (int i = 0; i < appWidgetIds.length; i++) {
Intent intentService = new Intent(context, WidgetService.class);
intentService.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
intentService.setData(Uri.parse(intentService.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews widget = new RemoteViews(context.getPackageName(), R.layout.widget);
widget.setRemoteAdapter(appWidgetIds[i], R.id.list_view, intentService);
Intent clickIntent = new Intent(context, MainActivity.class);
PendingIntent …
Run Code Online (Sandbox Code Playgroud) android android-widget appwidgetprovider android-appwidget android-6.0-marshmallow