AppWidget PendingIntent启动重启后无法正常工作

编程入门 行业动态 更新时间:2024-10-28 09:18:11
本文介绍了AppWidget PendingIntent启动重启后无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有2个挂起意图的AppWidget。他们工作的大部分时间,但他们停止响应一段时间后。我唯一​​能找出的是,他们残缺的一个发射器重新启动后,也就是我用发射器临,有时摆弄设置并重新启动它。之后,他们没有在所有工作。

I have an AppWidget with 2 pending intents. They work most of the time, but after a while they stop responding. Only thing I have been able to pinpoint is that they are crippled after a Launcher restart, i.e. I use Launcher Pro, and sometimes fiddle with settings and have to restart it. After that they don't work at all.

下面是我的 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); } }

我看了一下把意图在服务,但尝试失败。任何帮助是AP preciated。

I have read about putting the intents in a service, but have tried and failed. Any help is appreciated.

推荐答案

正如我写的 你应该只生产了远程视窗的一个实例。

As I've written here you should only produce a single instance of the RemoteView.

更多推荐

AppWidget PendingIntent启动重启后无法正常工作

本文发布于:2023-11-27 04:48:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636664.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重启   无法正常   工作   AppWidget   PendingIntent

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!