窗口小部件后,“清除内存”不工作部件、内存、窗口、工作

2023-09-08 10:05:05 作者:不提从前

现在的问题是,在我使用内置的任务管理器的清洁内存/ RAM,我的小工具将停止工作。我想这是关系到清洁RAM.After了大量的研究和一些尝试的任务管理器的方法,我发现我需要 BroadcastReciever听包装的更改和更新: 所以,我实现了,但它不工作,因为文件说的 重启动 / 清除 包没有收到该广播

在清单文件寄存器接收器:

 <接收器的Andr​​oid版本:NAME =com.app.lab.receiver.onRestartReciever>
    <意向滤光器>
        <作用机器人:名称=android.intent.action.PACKAGE_REPLACED/>
        <作用机器人:名称=android.intent.action.PACKAGE_RESTARTED/>
         <作用机器人:名称=android.intent.action.PACKAGE_DATA_CLEARED/>
        <数据机器人:计划=包/>
    &所述; /意图滤光器>
 

PACKAGE_REPLACED - 所谓的特别通知应用程序更新

PACKAGE_RESTARTED - 当大多数内存清洁工正在打扫memmory调用。

数据行用于监视所申请的具体包动作

  onRestartReciever延伸的BroadcastReceiver {public类
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
// TODO自动生成方法存根
Log.d(调试,onRestartReciever); //我没有得到这个日志从任务管理器清除内存



}
}
 
如何删除安卓系统的窗口小部件

我试图用虚拟服务来获得它的生命周期,即检查时的onDestroy叫,但我发现它不是一个可靠的方法,可能的onDestroy不能当应用程序由任务管理器杀调用。

所以最后,我的问题: 有没有办法告诉了Android系统重新启动appWidgets当任务管理器或操作系统清除内存的

注意: 我的窗口小部件只包含一个按钮启动一个Activity.It工作的时间最多,但会停止响应,如果操作系统本身清除内存或用户的任务,有力地做到这一点manager.I've下载了一些小部件的它似乎要继续清理内存之后也工作正常。

更新:要下我的问题,没有必要的经历完整的code这是简单的应用程序我的应用剂量不包含任何Activty或服务。只包含一个按钮,使敬酒message.There部件是仅有的两个班在我的应用程序(WidgetProvider和onRestartReciever)完蛋了 Widget类WidgetProvider.class

 公共类WidgetProvider扩展AppWidgetProvider {

私人RemoteViews remoteViews;
私人组件名watchWidget;
PendingIntent PI;
@覆盖
公共无效的OnUpdate(上下文的背景下,AppWidgetManager appWidgetManager,
        INT [] appWidgetIds){
    remoteViews =新RemoteViews(context.getPackageName()
            R.layout.touchwidget);
   意图toggleClickPlayer =新的意图(context.getApplicationContext(),WidgetProvider.class);
    toggleClickPlayer.setAction(玩家);
    PendingIntent toggleIntentPlayer = PendingIntent.getBroadcast(上下文,0,toggleClickPlayer,endingIntent.FLAG_CANCEL_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.player,toggleIntentPlayer);
    appWidgetManager.updateAppWidget(appWidgetIds,remoteViews);
}

@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
    super.onReceive(背景下,意图);
    watchWidget =新的组件名(背景下,WidgetProvider.class);
    remoteViews =新RemoteViews(context.getPackageName(),R.layout.touchwidget);
Toast.makeText(背景下,玩家开始,Toast.LENGTH_SHORT).show();
(AppWidgetManager.getInstance(上下文))updateAppWidget(watchWidget,remoteViews);
        }
    }
 }
 

解决方案

控件不绑定到应用程序的生命周期。所有的部件结合在一起。如果所有你的小工具有一个按钮不是没有理由为它停止工作。你的问题是别的地方。 出于某种原因,你的按钮意图无法启动,你将其设置为启动。

如果你能告诉我你的code设置按钮,我可以帮你多,但它是另外一个问题,你最好谷歌的答案,然后再发布。

编辑:看起来你并没有了解部件的想法。现在你的按钮做的是开始的小部件。它看起来怪我,我不知道究竟发生了那里......我建议你的按钮会开始一个全新的服务。这项服务将显示你的面包。你挑衅不需要监听重新启动包广播

The problem is that after I use the built in Task Manager's Clean Memory/Ram, My widget stops working .I guess this is related to the Task Manager's method of cleaning RAM.After a lot of research and some attempts, I found that i need BroadcastReciever to listen to package changes and updates: So i implemented but its not working because document says that the Restarted/Cleared package does not receive this broadcast

register receiver in the manifest file:

<receiver android:name="com.app.lab.receiver.onRestartReciever">
    <intent-filter>
        <action android:name="android.intent.action.PACKAGE_REPLACED" />
        <action android:name="android.intent.action.PACKAGE_RESTARTED" />
         <action android:name="android.intent.action.PACKAGE_DATA_CLEARED"/>
        <data android:scheme="package"  />
    </intent-filter>

PACKAGE_REPLACED - called in particular to notify application update.

PACKAGE_RESTARTED - called when most memory cleaners are cleaning memmory.

the "data" row is used to monitor action applied for the specific package.

public class onRestartReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.d("DEBUG", "onRestartReciever");//I am not getting this log on clearing memory from task manager



}
}

I tried to use dummy service to get its lifecycle ie to check when onDestroy is called but what I found it not a reliable way ,onDestroy may not be called when application is killed by Task Manager.

So finally, my question : Is there any way to tell the android system to reStart appWidgets when Task manager or OS cleans memory .

Note: My widget contains only one button that launches an Activity.It works most of the time but stops responding if OS itself cleans memory or user forcefully do it from task manager.I've downloaded some of the widget it seem to continue working fine after cleaning memory also.

Update: To under my problem no need of going through complete code it is simple Application . My application dose not contain any Activty or Service. It contains only widget with one button which gives toast message.There is only two class in my application(WidgetProvider and onRestartReciever) thats it Widget class WidgetProvider.class

 public class WidgetProvider extends AppWidgetProvider {

private RemoteViews remoteViews;
private ComponentName watchWidget;
PendingIntent pi;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    remoteViews = new RemoteViews(context.getPackageName(),
            R.layout.touchwidget);
   Intent toggleClickPlayer = new Intent(context.getApplicationContext() ,WidgetProvider.class);
    toggleClickPlayer.setAction("PLAYER");
    PendingIntent toggleIntentPlayer = PendingIntent.getBroadcast(context,0, toggleClickPlayer,endingIntent.FLAG_CANCEL_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.player, toggleIntentPlayer);
    appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
    watchWidget = new ComponentName(context,WidgetProvider.class);
    remoteViews = new RemoteViews(context.getPackageName(),R.layout.touchwidget);
Toast.makeText(context, " Player started",Toast.LENGTH_SHORT).show();
(AppWidgetManager.getInstance(context)).updateAppWidget(watchWidget, remoteViews);
        }
    } 
 }

解决方案

Widget does not bound to application life cycle. All the widgets are bound together. If all what your widget got is a button than there is no reason for it to stop working. You problem is some place else. For some reason your button intent is fail to start what you set it to start.

If you show me your code for setting the button, I be able to help you more, but it is another question, and you better Google for answer before posting.

EDIT: It looks like you didn't understood the idea of widgets. Right now what your button is doing is starting the widget. It looks weird to me, I am not sure what exactly is happening there... I suggest that your button will start a completely new service. That service will show your toast. You defiantly do not need to listen for restart package broadcast.