uka*_*nth 7 android android-activity
我正在开发一个开源应用程序(droidwall fork),我遇到的问题之一是当系统重新启动时iptables规则没有正确应用.它适用于大多数Android版本.但是在某些特定的ROMS(CM 10.1)上,它提供了以下logcat
12-26 08:39:27.116 I/ActivityManager(582):
No longer want dev.ukanth.ufirewall (pid 2297): empty #17
Run Code Online (Sandbox Code Playgroud)
我的代码工作如下,
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);*/
}
Run Code Online (Sandbox Code Playgroud)
你可以在这里找到我的Api.java课程.
方法onReceived()
完成后,系统会假定您已完成BroadcastReceiver
并将托管进程设置为最低优先级.并且我们知道多士炉Handler的handleMessage()
方法是异步调用的,在onReceived()
方法完成后调用,并且无法保证该过程仍然存在以执行回调方法
在大多数Android版本中,系统启动时运行的进程数量 - up并不是那么多,你的进程(具有最低优先级)有机会保持活着,直到调用回调方法handleMessaged()
,但是使用ROMS(CM 10.1),可能有那么多进程在那个时刻运行,系统必须杀死更低优先进程以释放资源,以便更高优先级的进程可以正常运行,并且您的进程是一个被杀死的好方法.
我建议您启动一项服务来执行这些异步任务,这会使您的进程具有更高的优先级(默认情况下服务进程优先级或您可以使用startForeground()
方法获取前台进程优先级)
归档时间: |
|
查看次数: |
4861 次 |
最近记录: |