- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个带有 2 个待处理 Intent 的 AppWidget。他们大部分时间都在工作,但过一会儿他们就会停止响应。我唯一能够确定的是,在 Launcher 重新启动后它们会瘫痪,即我使用 Launcher Pro,有时会摆弄设置并不得不重新启动它。之后他们就完全不工作了。
这是我的 onRecieve()
和 onUpdate()
方法:
public void onReceive(Context context, Intent intent)
{
super.onReceive(context, intent);
String action = intent.getAction();
if(action.equals("android.tristan.widget.digiclock.CLICK"))
{
PackageManager packageManager = context.getPackageManager();
Intent alarmClockIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER);
String clockImpls[][] = {
{"HTC Alarm Clock", "com.htc.android.worldclock", "com.htc.android.worldclock.AlarmClock" },
{"Standar Alarm Clock", "com.android.deskclock", "com.android.deskclock.AlarmClock"},
{"Froyo Nexus Alarm Clock", "com.google.android.deskclock", "com.android.deskclock.DeskClock"},
{"Moto Blur Alarm Clock", "com.motorola.blur.alarmclock", "com.motorola.blur.alarmclock.AlarmClock"}
};
boolean foundClockImpl = false;
for(int i=0; i<clockImpls.length; i++) {
String vendor = clockImpls[i][0];
String packageName = clockImpls[i][1];
String className = clockImpls[i][2];
try {
ComponentName cn = new ComponentName(packageName, className);
ActivityInfo aInfo = packageManager.getActivityInfo(cn, PackageManager.GET_META_DATA);
alarmClockIntent.setComponent(cn);
foundClockImpl = true;
} catch (NameNotFoundException e) {
Log.d(LOGTAG, "Error," + vendor + " does not exist");
}
}
if (foundClockImpl) {
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(50);
final RemoteViews views = new RemoteViews(context.getPackageName(), layoutID);
views.setOnClickPendingIntent(R.id.TopRow, PendingIntent.getActivity(context, 0, new Intent(context, DigiClock.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK), PendingIntent.FLAG_UPDATE_CURRENT));
AppWidgetManager.getInstance(context).updateAppWidget(intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS), views);
alarmClockIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(alarmClockIntent);
}
}
}
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
context.startService(new Intent(UpdateService.ACTION_UPDATE));
context.startService(new Intent(context, ScreenUpdateService.class));
final int Top = appWidgetIds.length;
final int Bottom = appWidgetIds.length;
for (int i=0; i<Top; i++)
{
int[] appWidgetId = appWidgetIds;
final RemoteViews top=new RemoteViews(context.getPackageName(), layoutID);
Intent clickintent=new Intent("android.tristan.widget.digiclock.CLICK");
PendingIntent pendingIntentClick=PendingIntent.getBroadcast(context, 0, clickintent, 0);
top.setOnClickPendingIntent(R.id.TopRow, pendingIntentClick);
appWidgetManager.updateAppWidget(appWidgetId, top);
}
for (int i=0; i<Bottom; i++)
{
int[] appWidgetId = appWidgetIds;
RemoteViews bottom=new RemoteViews(context.getPackageName(), layoutID);
Intent clickintent=new Intent("android.tristan.widget.digiclock.CLICK_2");
PendingIntent pendingIntentClick=PendingIntent.getBroadcast(context, 0, clickintent, 0);
bottom.setOnClickPendingIntent(R.id.BottomRow, pendingIntentClick);
appWidgetManager.updateAppWidget(appWidgetId, bottom);
}
}
我读过有关将 Intent 放入服务的文章,但尝试过但失败了。感谢您的帮助。
最佳答案
正如我所写的here您应该只生成一个 RemoteView 实例。
关于android - 启动器重启后 AppWidget PendingIntent 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4909886/
我正在使用一个服务来更新我的 AppWidgets,该服务监听发布/订阅 channel 并且基本上永远在后台运行,每当收到更新时更新 AppWidgets。 我不担心电池生命周期,因为设备始终处于接
我想获取与应用小部件 ID 关联的应用小部件布局。 在 appwidget 的 Widget provider 类中,android 文档提供了一种获取 appwidget id 的简单方法... p
我在 eclipse 中有 2 个 android 项目,一个是主应用程序,另一个是主屏幕的小部件。我想在用户单击小部件时打开主应用程序。但是,当我使用来自主应用程序的 Activity 创建 Int
我在 Android 市场上发布了几个应用程序小部件,它们大部分都运行良好。但是我注意到使用共享首选项保存的首选项有时会重置为默认值。这种情况下的默认值是硬编码变量。保存在 xml 中的共享首选项与用
假设我有一个 android 项目,它有可以添加到应用程序启动器的可用 appWidget。该项目还有一个显示我创建的 appWidget 的 Activity 。 问题:对于启动器,appWidge
我创建了一个显示数字时钟的简单应用程序小部件,并为该时钟设置了一个 PendingIntent。(即)当用户点击 TextView 时,一个拨号盘被打开。它工作得很好。但是,当我打开一些应用程序(如
我有一个小部件,可以让您从 2 种尺寸中进行选择,它还有一个配置。由于某种原因,一段时间后,我的小部件上的按钮将解除绑定(bind),您将无法单击任何内容。我不知道为什么会这样。难道是我的 OnRec
我遇到的问题是,当我调试我的小部件时,它会更改布局,但当我只是运行它时,它不起作用。根据我的调试消息,它设置了 setOnClickPendingIntent 但它没有启动我的服务。 简而言之,我的小
从主屏幕删除我的最后一个 appWidget 实例后,onDisabled、onEnabled 不工作。 我将我的小部件注册为广播接收器,主屏幕上没有实例但广播接收,而且我看到我的日志写在我的 app
我有一个 appwidget,我想在整个 appwidget 布局上添加一个点击监听器,而不仅仅是在 TextView ecc 上。这是行不通的 remoteViews.setOnClickPendi
我一直致力于实现一个可以手动更新的 AppWidget。但是,出于某种原因,Widget 除了第一次显示时不会更新,并且该更新是通过 Configuration Activity 进行的,它与代码几乎
当我的主要 Activity 暂停时,我需要更新我的 AppWidget。我尝试在主要 Activity 的 onPause() 中使用以下代码: super.onPause(); AppWidget
编辑:我安装了我的应用程序 4.x 设备,没有问题。问题仅存在于 3.x 设备。 我正在尝试在用户设置时手动更新 AppWidget设备上的更改。为此,我使用与此类似的代码: ComponentNam
我正在 android 上创建一些应用程序小部件。我还想重新使用 appwidgets 的布局,这样我就可以在我的 Activity 中进行控制,使其看起来一样。 为了工作正常,我遇到的问题是我希望得
我在编写启动器时遇到了 appwidgets 的问题。只有 Clock widget 正常工作,其他不正常: 部分代码: final AppWidgetHostView hostView = mApp
当我在 Debug模式下运行时,我似乎无法到达服务内部的任何断点,这是为什么? @Override public void onUpdate(Context context, AppWidge
我创建了一个显示简单 TextView 的小部件,它可以在配置 Activity 中作为 Edittext 进行编辑。我使用共享首选项保存输入的文本,因此用户可以点击小部件来编辑文本,并且已经输入的文
我有一个应用程序小部件的问题。它没有配置 Activity 。当我在模拟器中设置 android:updatePeriodMillis="10000" (知道不应该这样做,但它只是为了测试)时 onU
您好,我的 Android 小部件出现了一个非常奇怪的问题,我仔细查看了很多地方,但我似乎无法弄清楚哪里出了问题。基本上我在我的小部件中调用了一个 pendingintent 广播,并在 onreci
这就是我想从我的 AppWidget 中得到的: 配置 Activity 出现,当小部件被添加到屏幕时//到目前为止一切顺利 保存配置后,启动更新小部件的服务//目前为止一切正常 定期安排警报以运行更
我是一名优秀的程序员,十分优秀!