从在UI线程中运行代码的角度来看,之间有什么区别:
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
Log.d("UI thread", "I am the UI thread");
}
});
Run Code Online (Sandbox Code Playgroud)
要么
MainActivity.this.myView.post(new Runnable() {
public void run() {
Log.d("UI thread", "I am the UI thread");
}
});
Run Code Online (Sandbox Code Playgroud)
和
private class BackgroundTask extends AsyncTask<String, Void, Bitmap> {
protected void onPostExecute(Bitmap result) {
Log.d("UI thread", "I am the UI thread");
}
}
Run Code Online (Sandbox Code Playgroud) 我用远程接口编写了一个服务,并将其安装在我的PC的Eclipse AVD上.我有一个客户端测试工具,它启动并调用服务中的方法.最初我通过控件类和活动安装了服务,我现在已将其删除,因此服务的清单如下所示:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myname.gridservice"
android:versionCode="1"
android:versionName="1.0">
<application
android:icon="@drawable/icon"
android:label="@string/app_name"
android:debuggable="true">
<service
android:enabled="true"
android:debuggable="true"
android:name="OverlayService">
<intent-filter>
<action android:name="com.myname.OverlayService.SERVICE"/>
<action android:name="com.myname.gridservice.IRemoteInterface" />
</intent-filter>
</service>
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
所以没有活动标签.
当我从Eclipse中的调试图标启动它时,控制台告诉我它正在安装apk(它是),但它不会显示为调试线程并且不会触发断点,尽管服务的行为是可以的客户看到了它.如果我将服务标签包装在具有关联类的活动标签中并启动它,那么我可以调试它
是否可以调试服务而不将其包装在活动中?
哪个是使用处理程序的更好方法.任何优点.我遇到的所有示例似乎都给出了内联版本.
Handler.Callback在类中使用implements 并实现接口方法.
要么
使用内联代码版本
private Handler mHandler = new Handler(){ ....};
Run Code Online (Sandbox Code Playgroud)