7

I am working on a open source application (droidwall fork) and i am stuck with one of the issue were the iptables rules were not applied properly when the system reboots. it works perfectly on most of the android versions. But on some specific ROMS (CM 10.1) it gives the following logcat

12-26 08:39:27.116 I/ActivityManager(582): 
No longer want dev.ukanth.ufirewall (pid 2297): empty #17

My code works somethings like below,

private Handler mHandler = new Handler(Looper.getMainLooper());
@Override
public void onReceive(final Context context, final Intent intent) {
    if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
        if (Api.isEnabled(context.getApplicationContext())) {
            final Handler toaster = new Handler() {
                public void handleMessage(Message msg) {
                    if (msg.arg1 != 0) Toast.makeText(context, msg.arg1, Toast.LENGTH_SHORT).show();
                }
            };

            mHandler.post(  
            // Start a new thread to enable the firewall - this prevents ANR
            new Runnable() {
                @Override
                public void run() {
                    if (!Api.applySavedIptablesRules(context.getApplicationContext(), false)) {
                        // Error enabling firewall on boot
                        final Message msg = new Message();
                        msg.arg1 = R.string.toast_error_enabling;
                        toaster.sendMessage(msg);
                        Api.setEnabled(context.getApplicationContext(), false, false);
                    }
                }
            });
            // Start a new thread to enable the firewall - this prevents ANR
        }
        /*Intent i = new Intent(context, StartupService.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startService(i);*/
    }

You can find my Api.java class here.

4

3 回答 3

8

12-26 08:39:27.116 I/ActivityManager(582): 不再需要 dev.ukanth.ufirewall (pid 2297): empty #17

此日志意味着您已达到允许的最大空进程。(在你的情况下,16 是最大值)

更多关于android doc 中的空进程

不包含任何活动应用程序组件的进程。保持这种进程处于活动状态的唯一原因是出于缓存目的,以便在下次组件需要在其中运行时缩短启动时间。系统通常会杀死这些进程,以平衡进程缓存和底层内核缓存之间的整体系统资源。

因此,不确定您拥有的日志是否与您的 iptables 规则问题直接相关。

于 2012-12-26T17:10:51.790 回答
6

方法onReceived()完成后,系统假定您已完成BroadcastReceiver并将托管进程设置为最低优先级。而且我们知道toaster Handler的handleMessage()方法是异步调用的,在onReceived()方法结束后调用,并不能保证进程还在执行回调方法

在大多数Android版本上,系统启动时运行的进程数- up 不是很多,并且您的进程(优先级最低)有机会在回调方法之前保持活动状态handleMessaged()被调用,但使用 ROMS (CM 10.1),可能有太多进程在运行,系统必须杀死低优先级进程以释放资源,以便高优先级进程可以正常运行,您的进程是一个很好的候选者被杀。

我建议您启动一个服务来执行那些异步任务,这使您的进程具有更高的优先级(默认为服务进程优先级,或者您可以使用startForeground()方法来获取前台进程优先级)

于 2013-01-29T10:06:26.583 回答
0

也许您可以延迟使用 mHandler.post 来测试您的案例,例如 20 秒?

于 2013-01-29T11:46:07.243 回答