Android部件点击不会做几个小时后什么 [英] Android Widget click doesn't do anything after few hours
问题描述
我有了一个刷新按钮和一个TextView小部件。刷新更新内容,当用户点击TextView中启动一个新的活动。
但问题是它正常工作了几个小时,然后的onclick和刷新按钮不会做任何事情。没有被捕获在logcat中。此外,如果用户删除插件,并把一个新的就开始工作了几个小时,然后同样的故事:(...我在做什么错
广播接收器。
的onupdate
公共无效的OnUpdate(上下文的背景下,AppWidgetManager appWidgetManager,
INT [] appWidgetIds){
长间隔= getrefresInterval();
最终意向意图=新的意图(背景下,UpdateService.class);
最终PendingIntent未决= PendingIntent.getService(上下文,0,意图,0);
最后AlarmManager报警=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(待定);
alarm.setRepeating(AlarmManager.ELAPSED_REALTIME,SystemClock.elapsedRealtime(),间隔时间,待定);
//构建意图调用服务
RemoteViews remoteViews =新RemoteViews(context.getPackageName(),R.layout.widget);
//要处理发生的点击,我们必须使用挂起的意图作为
// onClickListener由主屏幕应用excecuted
意图ClickIntent =新的意图(context.getApplicationContext(),widgetHadith.class);
意图UpdateIntent =新的意图(context.getApplicationContext(),UpdateService.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context.getApplicationContext(),0,ClickIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntentUpdate = PendingIntent.getService(context.getApplicationContext(),0,UpdateIntent,
PendingIntent.FLAG_UPDATE_CURRENT); //使用这个来更新小窗口的文本。如果用这个把UpdateService.class到意向
remoteViews.setOnClickPendingIntent(R.id.widget_textview,pendingIntent);
remoteViews.setOnClickPendingIntent(R.id.widget_refresh,pendingIntentUpdate);
//最后与有关click监听器的信息更新所有部件
appWidgetManager.updateAppWidget(appWidgetIds,remoteViews);
//更新通过服务窗口小部件
context.startService(意向);
}
的onReceive
公共无效的onReceive(上下文的背景下,意图意图){
// V1.5修复程序不叫onDelete行动
最后弦乐行动= intent.getAction();
如果(AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(动作)){
最终诠释appWidgetId = intent.getExtras()调用getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
如果(appWidgetId!= AppWidgetManager.INVALID_APPWIDGET_ID){
this.onDeleted(背景下,新的INT [] {appWidgetId});
}
} 其他 {
super.onReceive(背景下,意图);
}
}
onDelete
公共无效onDeleted(上下文的背景下,INT [] appWidgetIds){
// Toast.makeText(上下文中,onDelete,Toast.LENGTH_SHORT).show();
super.onDeleted(背景下,appWidgetIds);
}
服务的OnStart我在哪里更新
@覆盖
公共无效ONSTART(意向意图,诠释startId){
RemoteViews updateViews =新RemoteViews(this.getPackageName(),R.layout.widget);
processDatabase();
跨区文本= LoadHadith();
字符串圣训= text.toString();
Log.d(BR,服务--->中);
//设置组件的TextView的id为信息的文本
updateViews.setTextViewText(R.id.widget_textview,文本);
//推送更新这个小部件到主屏幕
组件名thisWidget =新的组件名(本,HelloWidget.class);
AppWidgetManager经理= AppWidgetManager.getInstance(本);
manager.updateAppWidget(thisWidget,updateViews);
}
现在的问题是,你不能对一个插件做了partiall更新,您必须将所有的小部件功能,如设置PendingIntent的每一次你推一个新的远程视窗。 (Partiall更新只适用于API14及以上...)。
你的部件正在失去他们的pendingIntents的原因是,Android系统节省了远程视窗,并重建小部件用它,在情况下,它重置窗口小部件(不足memmory中,任务管理器/ taskKiller在使用,等...),所以你必须将所有的更新$ C $下在你的updateService的远程视窗的部件。 否则,它只是不会再设置pendingIntents。
所以只需添加code中的pendingIntents设置为服务,您的问题将得到解决=]
I have a widget that has a refresh button and a textview. Refresh updates the content and when user clicks on textview it starts a new activity.
Problem is it works fine for a few hours and then onclick and refresh button doesn't do anything. Nothing is captured in logcat. Also If user deletes widget and put a new one it starts working for a few hours and then the same story :(...what am I doing wrong!
Broadcast receiver.
onUpdate
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
long interval = getrefresInterval();
final Intent intent = new Intent(context, UpdateService.class);
final PendingIntent pending = PendingIntent.getService(context, 0, intent, 0);
final AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pending);
alarm.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),interval, pending);
// Build the intent to call the service
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget);
// To react to a click we have to use a pending intent as the
// onClickListener is excecuted by the homescreen application
Intent ClickIntent = new Intent(context.getApplicationContext(),widgetHadith.class);
Intent UpdateIntent = new Intent(context.getApplicationContext(),UpdateService.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context.getApplicationContext(), 0, ClickIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntentUpdate = PendingIntent.getService(context.getApplicationContext(), 0, UpdateIntent,
PendingIntent.FLAG_UPDATE_CURRENT); //use this to update text on widget. if use this put UpdateService.class to intent
remoteViews.setOnClickPendingIntent(R.id.widget_textview, pendingIntent);
remoteViews.setOnClickPendingIntent(R.id.widget_refresh, pendingIntentUpdate);
// Finally update all widgets with the information about the click listener
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
// Update the widgets via the service
context.startService(intent);
}
onReceive
public void onReceive(Context context, Intent intent) {
// v1.5 fix that doesn't call onDelete Action
final String action = intent.getAction();
if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
final int appWidgetId = intent.getExtras().getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
this.onDeleted(context, new int[] { appWidgetId });
}
} else {
super.onReceive(context, intent);
}
}
onDelete
public void onDeleted(Context context, int[] appWidgetIds) {
// Toast.makeText(context, "onDelete", Toast.LENGTH_SHORT).show();
super.onDeleted(context, appWidgetIds);
}
Service onstart where I am updating
@Override
public void onStart(Intent intent, int startId) {
RemoteViews updateViews = new RemoteViews(this.getPackageName(),R.layout.widget);
processDatabase();
Spanned text = LoadHadith();
String hadith = text.toString();
Log.d("BR", "service---> ");
// set the text of component TextView with id 'message'
updateViews.setTextViewText(R.id.widget_textview, text);
//Push update for this widget to the home screen
ComponentName thisWidget = new ComponentName(this, HelloWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, updateViews);
}
The problem is that you can't do a partiall update for a widget, you must set all the widget features, such as the set of PendingIntent's every time you push a new remoteView. (Partiall updates are only available for API14 and up...).
The reason your widgets are loosing their pendingIntents is that the android system saves the remoteView, and rebuilds your widget with it, in case it resets the widget (shortage of memmory, TaskManager/taskKiller in use, etc...), so you must set all the update code for the widget in the remoteView in your updateService. Otherwise, it's just won't set the pendingIntents again.
So just add the code setting the pendingIntents to the service and your problem will be solved =]
这篇关于Android部件点击不会做几个小时后什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!