1

我正在使用 IntentService 定期执行任务。每次调用此服务后,都会留下三个额外的打开文件。

在 adb 控制台上,此命令显示 PID(进程 ID)的所有打开文件:

lsof | busybox grep PID | busybox wc -l

活动:

public class FileLeakActivity extends Activity {
    private Handler handler = new Handler();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        handler.post(mStartService);
    }

    private Runnable mStartService = new Runnable() {
        @Override
        public void run() {
            Intent svc = new Intent(FileLeakActivity.this, FileLeakService.class);
            FileLeakActivity.this.startService(svc);
            handler.postDelayed(mStartService, 1000);
        }
    };
}

服务:

public class FileLeakService extends IntentService {
    private final static String TAG = FileLeakService.class.getSimpleName();

    public FileLeakService() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(Intent arg0) {
        Log.v(TAG, "onHandleIntent");
    }
}

显现:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sample.android.fileleak"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >

        <service android:name="FileLeakService" />

        <activity
            android:name=".FileLeakActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

我究竟做错了什么?

4

0 回答 0