这篇文章主要围绕android–AppWidget只更新一次和androidwidget能卸载掉吗展开,旨在为您提供一份详细的参考资料。我们将全面介绍android–AppWidget只更新一次的优缺
这篇文章主要围绕android – AppWidget只更新一次和androidwidget能卸载掉吗展开,旨在为您提供一份详细的参考资料。我们将全面介绍android – AppWidget只更新一次的优缺点,解答androidwidget能卸载掉吗的相关问题,同时也会为您带来android appwidget listview没有更新、Android Appwidget textview不更新、android appwidget 笔记、Android AppWidgetProviderInfo图标ID的实用方法。
本文目录一览:- android – AppWidget只更新一次(androidwidget能卸载掉吗)
- android appwidget listview没有更新
- Android Appwidget textview不更新
- android appwidget 笔记
- Android AppWidgetProviderInfo图标ID
android – AppWidget只更新一次(androidwidget能卸载掉吗)
我有appwidget的问题.它没有配置活动.当我在模拟器中设置android:updatePeriodMillis =“10000”(知道不应该这样做,但它只是用于测试)时,每10秒调用一次onUpdate(…)函数.但是当我在我的真实手机上安装我的应用程序(motorola里程碑)onUpdate(…)仅在我将小部件添加到主屏幕时调用.再也不会了.
任何想法为什么会在真实设备上发生,而不是在模拟器中?
解决方法:
你用的是什么Sdk?
自Android sdk 1.6以来,小部件更新最短时间为30分钟.
android appwidget listview没有更新
Intent update = new Intent(context,MenuWidgetProvider.class); update.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); update.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,appWidgetIds); PendingIntent pIUpdate = PendingIntent.getbroadcast(context,update,0); ((AlarmManager) context.getSystemService(Context.ALARM_SERVICE)). set(AlarmManager.RTC,nextTime.toMillis(false),pIUpdate); Log.d(TAG,"updating: " + dmt.mealName); for (int i = 0; i < appWidgetIds.length; ++i) { int widgetId = appWidgetIds[i]; RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.widget_menu); // meal name views.setTextViewText(R.id.widget_locname,prefs.getString(PREF_WIDGET_LOCNAME,"")); // adapter Intent adapter = new Intent(context,MenuWidgetAdapterService.class); adapter.putExtra(EXTRA_LOCATIONID,prefs.getInt(PREF_WIDGET_LociD,-1)); adapter.putExtra(EXTRA_MEALNAME,dmt.mealName); adapter.putExtra(EXTRA_DATE,dmt.date.toString()); views.setRemoteAdapter(R.id.widget_list,adapter); // update manager.updateAppWidget(widgetId,views); } super.onUpdate(context,manager,appWidgetIds);
奇怪的是,当更新发生时,onUpdate()方法运行 – 我看到Log.d调用的输出 – 但我的RemoteViewsFactory中没有调用onDataSetChanged().即使我调用notifyAppWidgetViewDataChanged,也没有效果.
解决方法
我是如何解决它的:
在你的WidgetProviderClass中
adapter.setData(Uri.fromParts("content",String.valueOf(appWidgetIds[i]+randomNumber),null));
randomNumber是一个随机数(WidgetProvider类的公共静态成员).
然后在RemoteViewsFactory类中,执行:
appWidgetId = Integer.valueOf(intent.getData().getSchemeSpecificPart()) - WidgetProvider.randomNumber;
这让“服务”认为它必须为另一个小部件创建一个Factory,因此创建一个带有更新值的新工厂,该工厂附加到您的小部件.
希望这可以帮助.
Android Appwidget textview不更新
但是在onRecive方法中,当我尝试使用RemoteViews为我的组件设置文本时,文本不会更新,也不会调用任何错误.我在下面附上了我的代码,任何帮助都会很棒.
谢谢,
中号
package com.android.FirstWidget;import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.appwidget.appwidgetprovider; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.ImageButton; import android.widget.RemoteViews; public class Exampleappwidgetprovider extends appwidgetprovider { @Override public void onReceive(Context context,Intent intent) { // Todo Auto-generated method stub Log.e("ds",intent.getAction()); Log.e("f","f"); if(intent.getAction().contains("1")){ RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.wid); views.setTextViewText(R.id.textView1,"heyheyhey"); Log.e("fssss","sssf"); } super.onReceive(context,intent); } public void onUpdate(Context context,AppWidgetManager appWidgetManager,int[] appWidgetIds) { final int N = appWidgetIds.length; // Perform this loop procedure for each App Widget that belongs to this provider for (int i=0; i<N; i++) { int appWidgetId = appWidgetIds[i]; // Create an Intent to launch ExampleActivity Intent intent = new Intent(context,Exampleappwidgetprovider.class); intent.setAction("1"); PendingIntent pendingIntent = PendingIntent.getbroadcast(context,intent,0); // Get the layout for the App Widget and attach an on-click listener // to the button RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.wid); views.setonClickPendingIntent(R.id.arrowLeft,pendingIntent); views.setonClickPendingIntent(R.id.arrowRight,pendingIntent); //views.set // Tell the AppWidgetManager to perform an update on the current app widget appWidgetManager.updateAppWidget(appWidgetId,views); } } }
解决方法
package com.android.FirstWidget;import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.appwidget.appwidgetprovider; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.ImageButton; import android.widget.RemoteViews; public class Exampleappwidgetprovider extends appwidgetprovider { @Override public void onReceive(Context context,Intent intent) { // Todo Auto-generated method stub Log.e("ds",intent.getAction()); Log.e("f","f"); RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.wid); if(intent.getAction().contains("arrow_left")){ views.setTextViewText(R.id.textView1,"left"); Log.e("fssss","sssf"); } else if(intent.getAction().contains("arrow_right")){ views.setTextViewText(R.id.textView1,"right"); Log.e("fssss","sssf"); } else { super.onReceive(context,intent); } ComponentName componentName = new ComponentName(context,Exampleappwidgetprovider.class); AppWidgetManager.getInstance(context).updateAppWidget(componentName,views); } public void onUpdate(Context context,Exampleappwidgetprovider.class); intent.setAction("arrow_left"); PendingIntent pendingIntent = PendingIntent.getbroadcast(context,pendingIntent); intent = new Intent(context,Exampleappwidgetprovider.class); intent.setAction("arrow_right"); pendingIntent = PendingIntent.getbroadcast(context,views.setonClickPendingIntent(R.id.arrowRight,views); } } }
此外,虽然我相信你编码的内容应该可以工作,但如果没有,你可以尝试在清单文件中设置按钮的intent操作.它应该是这样的
<intent-filter > <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/> <action android:name="com.android.FirstWidget.arrow_left"/> <action android:name="com.android.FirstWidget.arrow_right"/> </intent-filter>
我想现在应该可以了.我自己偶然发现了这个问题,并在此找到了解决方案:Clickable widgets in android
android appwidget 笔记
AppWidget 就是我们使用的窗口小部件
实现 appwidget 非常简单,只需要一下几个步骤就 OK
1. 在 /res/layout 中建立一个 布局文件。此布局文件就是 窗体小部件 的 "样子"
2. 在 /res/xml 中建立一个 xml 文件此文件对 appwidget 进行配置。如下
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="1dp"
android:minHeight="1dp"
android:updatePeriodMillis="4000000"
android:initialLayout="@layout/appwidgetlayout">
<!-- 从上到下
宽
高
更新毫秒数
appwidget的初始化的布局 引用 res/layout 中的布局
-->
</appwidget-provider>
3. 实现一个类继承 AppWidgetProvider
public class AppWeigetTest extends AppWidgetProvider {
private static final String in="joker.broadcast.appwedget";
//当到达指定的更新时间之后,或用户添加窗口小部件时触发
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
//当用户删除窗体部件时触发
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
// TODO Auto-generated method stub
}
//当所有船体部件删除时触发
@Override
public void onDisabled(Context context) {
// TODO Auto-generated method stub
}
//接收到广播的时候触发,以上几个方法是使用这个方法转发的,也就是说其实窗口的各种"事件" 都是使用广播的形式发出的
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onReceive(context, intent);
}
}
4. 在 AndroidManifest.xml
<receiver android:name=".AppWeigetTest">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/appwidget_info"/>
</receiver>
以上 intent-filter 和 meta-data 是系统设置不变的,其中 meta-data 的 resource 下的文件是 appwidget 的配置文件
二,Appwidget 中常用类的使用:
1.PendingIntent
此类是包装 Intent 类的 使 Intent 在特定的事件触发
PendingIntent包装Intent意图 初始化方法有:getActivity getBroadcast getService
//参1 上下文对象 参数3 Intent对象
PendingIntent pending=PendingIntent.getActivity(context, 0, inten, 0);
2.RemoteViews
此类可以对:"小部件" 上的控件进行操作 (比如改变 TextView 上的文字 ImageView 上的图片操作,改变图片添加事件)
RemoteViews remote=new RemoteViews(context.getPackageName(), R.layout.appwidgetlayout);
//为窗口小部件上的控件添加点击事件
//参数一: 指定窗口小部件上的控件ID
//参数2 :指定触发事件后执行的Intent 的PendingIntent
remote.setOnClickPendingIntent(R.id.button1, pending); //像button1添加一个点击事件
3 AppWidgetManager
在 onXXX 事件中做为参数传递进来的,此类的对象负责更新 Appwidget
AppWidgetManager manager=AppWidgetManager.getInstance(context);
ComponentName name=new ComponentName(context, AppWeigetTest.class);//代表AppWeiget控件
//以上两个步骤是在没有appwidgetmanager对象的时候,比如在onReceive方法里如何使用
manager.updateAppWidget(name, remoteViews); //此方法更新appwidget并且执行remoteViews只有执行这个方法后remoteViews配置的信息才会生效
Android AppWidgetProviderInfo图标ID
如何解决Android AppWidgetProviderInfo图标ID?
| Android的“ 0”类包含一个整数“ 1”字段。我想将图标保留为Drawable
。
我认为图标ID需要针对某些软件包的ѭ3进行解析。窗口小部件提供程序不是我的应用程序的一部分,因此getResources()
返回的我自己的应用程序资源无用。 resources5ѭ也没有给出系统资源。
如何将这个整数id转换为Drawable?
解决方法
PackageManager
和getResourcesForApplication()
。
,就像CommonsWare所说的那样,您将需要使用getResourcesForApplication()
这是您可以使用它的方法,而且由于棒棒糖有一个loadIcon()函数,使用起来更容易
Drawable icon = null;
if (Build.VERSION.SDK_INT >= 21) {
icon = info.loadIcon(this,density);
} else {
try {
String packageName = info.provider.getPackageName();
Resources resources = context.getPackageManager().getResourcesForApplication(packageName);
icon = resources.getDrawable(info.icon);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
我们今天的关于android – AppWidget只更新一次和androidwidget能卸载掉吗的分享就到这里,谢谢您的阅读,如果想了解更多关于android appwidget listview没有更新、Android Appwidget textview不更新、android appwidget 笔记、Android AppWidgetProviderInfo图标ID的相关信息,可以在本站进行搜索。
本文标签: