我在onreceive方法中响应我的appwidget上的按钮按下.当我按下按钮时,我想强制小部件调用onupdate方法.我该如何做到这一点?
提前致谢!
我有一个应用程序小部件,其中只包含一个imageview.我重绘该图像并将其作为png存储在应用程序的私有内存中,然后我使用uri(widget.setImageViewUri(R.id.widget_icon,uri))设置RemoteViews的图像,而不是自己发送位图,因为非常罕见我得到的情况!失败的粘合剂交易!
问题是,随着图像随时间的变化,窗口小部件的图像不会改变.我用root explorer检查了保存的png并且它已正确更新.但不显示正确的图像.每个小部件显示从添加到主屏幕时的图像.如果我使用setImageViewBitmap(...)它可以正常工作,除了罕见的失败的活页夹事务.
我使用以下方法从服务更新小部件.它不适用于Android 2.3.7以及运行2.2的模拟器.可能是什么问题呢?是以某种方式缓存?
public void updateWidget(Context context) {
// redraws the widget's image
updateIcon();
OutputStream stream;
try {
stream = context.openFileOutput("icon.png", Context.MODE_WORLD_READABLE);
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
return;
}
m_Icon.compress(CompressFormat.PNG, 100, stream);
try {
stream.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, WidgetProvider.class));
Intent intent = new Intent(context, MyDialog.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent clickIntent = PendingIntent.getActivity(context, 0, intent, 0);
File file = new File(context.getFilesDir().getAbsolutePath() + File.separator + "icon.png");
// Uri …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) 我有一个widget类(扩展AppWidgetProvider),它在widget的布局中只有一个视图(ImageView).当用户点击小部件时,它会更新并启动一个没有问题的活动.小部件更新后,小部件每30分钟更新一次并启动活动.我的问题是:如何将窗口小部件配置为仅在自动更新时更新自身(而不是通过用户单击)?
这是我的代码:
public class Widget extends AppWidgetProvider {
private static final String ACTION_UPDATE = AppWidgetManager.ACTION_APPWIDGET_UPDATE;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
ComponentName cn = new ComponentName(context, Widget.class);
appWidgetManager.updateAppWidget(cn, remoteView(context));
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (intent.getAction().equalsIgnoreCase(ACTION_UPDATE)) {
ComponentName cn = new ComponentName(context, Widget.class);
AppWidgetManager.getInstance(context).updateAppWidget(cn,
remoteView(context));
Intent launch = new Intent(context, Main.class);
PendingIntent pi = PendingIntent.getActivity(context, 0, launch, 0);
try {
pi.send();
} catch (CanceledException e) {
e.printStackTrace();
}
}
} …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) 我的小部件的布局文件从顶层开始,具有:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/widget_margin"
android:id="@+id/widget"
android:background="#20000000" >
Run Code Online (Sandbox Code Playgroud)
因此,起始背景是半透明的.然后在执行期间的某个时刻,我调用以下内容使背景完全透明:
remoteViews.setInt(R.id.widget, "setBackgroundColor", Color.parseColor("#00000000"));
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
Run Code Online (Sandbox Code Playgroud)
虽然这是有效的,但我发现,当添加另一个小部件时,该小部件的背景将根据布局xml以完全透明而非半透明的方式开始.就好像为一个小部件设置背景颜色已经改变了它,包括随后添加的新小部件.
现在这很奇怪,因为与上面的代码片段同时,我还在窗口小部件中添加了一个位图(通过RemoteViews):
remoteViews.setImageViewBitmap(R.id.imageView, bmp);
Run Code Online (Sandbox Code Playgroud)
...并且该位图不会出现在所有后续窗口小部件中,因此我使用的基本方法似乎是合理的.
那么为什么背景会传播到其他小部件呢?
编辑添加appWidgetId评论中要求的派生:
基本上来自于Intent例如小部件onCreate()的配置Activity:
Intent intent = getIntent();
Bundle extras = intent.getExtras();
int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
Run Code Online (Sandbox Code Playgroud) 我需要在Android AppWidget中确定运行时的屏幕密度.我已经设置了一个HDPI仿真器设备(avd).如果设置了常规可执行项目,并将此代码插入onCreate方法:
DisplayMetrics dm = getResources().getDisplayMetrics();
Log.d("MyTag", "screen density " + dm.densityDpi);
Run Code Online (Sandbox Code Playgroud)
这按预期输出"屏幕密度240".
但是,如果我设置了一个AppWidget项目,并将此代码插入onUpdate方法:
DisplayMetrics dm = context.getResources().getDisplayMetrics();
Log.d("MyTag", "screen density " + dm.densityDpi);
Run Code Online (Sandbox Code Playgroud)
这输出"屏幕密度160".我注意到,连接调试器,这里的Resources对象的mDefaultDisplay成员在AppWidget案例中为null.
类似地,如果我在运行时使用从AppWidget中的context.getResources()获得的Resources对象获取资源,它将根据屏幕密度返回错误的资源.例如,我有一个60x60px可绘制的mdpi,以及一个80x80可绘制的hdpi.如果我使用context.getResources().getDrawable(...)获取此Drawable对象,则返回60x60版本.
有没有办法在运行时从AppWidget的上下文正确处理资源?
谢谢!
我使用我的应用程序的集合创建了一个应用程序小部件,该小部件显示该特定日期的日期和项目列表.一切正常,窗口小部件正在根据需要进行更新.但有时,通过单击下一个和上一个按钮更改窗口小部件中的日期时会发生什么,列表不刷新意味着项目在该特定日期未更新.此行为是随机的,有时仅发生.那么为什么会发生这种情况,我的代码中有任何错
我使用的代码是:
WidgetProvider.class
public class WidgetProvider extends AppWidgetProvider
{
private ThemeManager m_ThemeManagerObject;
private static String WIDGET_NEXT_BUTTON = "in.test.widgetApp.WIDGET_NEXT_BUTTON";
private static String WIDGET_PREV_BUTTON = "in.test.widgetApp.WIDGET_PREV_BUTTON";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
super.onUpdate(context, appWidgetManager, appWidgetIds);
// Set Date to current Date
NoteManager.getSingletonObject().setWidgetToCurrentDate();
// Code to update the widget by current date
updateAppWidget(context, AppWidgetManager.getInstance(context), appWidgetIds);
}
@Override
public void onReceive(Context context, Intent intent)
{
super.onReceive(context, intent);
int numOfDays = 1;
ComponentName thisWidget = new ComponentName(context, WidgetProvider.class);
AppWidgetManager appWidgetManager = …Run Code Online (Sandbox Code Playgroud)