首先,我对Android和JAVA世界(来自C/C++/Objective-C)非常陌生。我正在尝试集成Android bump API (3.0,最新版本),但遇到了麻烦。我复制了这个例子,它在Android2.2下工作得很好,凹凸服务也正常启动,但对于Android3.0和更高版本,它不能工作。当加载我的活动时,我得到了一个异常(主线程1上的网络),我知道这个异常以及如何避免它,但在这种情况下,Bump声明他们在自己的线程中运行他们的API,所以我真的不知道为什么我会得到它。他们说你不需要运行线程或任务。
以下是我的活动示例
public class BumpActivity extends Activity {
private IBumpAPI api;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bump);
bindService(new Intent(IBumpAPI.class.getName()), connection,
Context.BIND_AUTO_CREATE);
IntentFilter filter = new IntentFilter();
filter.addAction(BumpAPIIntents.CHANNEL_CONFIRMED);
filter.addAction(BumpAPIIntents.DATA_RECEIVED);
filter.addAction(BumpAPIIntents.NOT_MATCHED);
filter.addAction(BumpAPIIntents.MATCHED);
filter.addAction(BumpAPIIntents.CONNECTED);
registerReceiver(receiver, filter);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
private final ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder binder) {
Log.i("BumpTest", "onServiceConnected");
api = IBumpAPI.Stub.asInterface(binder);
try {
api.configure("API_KEY", "Bump User");
} catch (RemoteException e) {
Log.w("BumpTest", e);
}
Log.d("Bump Test", "Service connected");
}
@Override
public void onServiceDisconnected(ComponentName className) {
Log.d("Bump Test", "Service disconnected");
}
};
}听起来像是在api.configure的连接服务过程中出现了问题...我应该在单独的线程中还是在它自己的AsynchTask中运行它,但是如何运行以及为什么运行呢?
发布于 2012-07-05 17:09:37
我在这个问题上纠结了一天左右...从字面上讲,在这里发布了2分钟后,我解决了它……我只是把api.configure放在一个单独的线程上(比AsynchTask短)。
private final ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder binder) {
Log.i("BumpTest", "onServiceConnected");
api = IBumpAPI.Stub.asInterface(binder);
new Thread() {
public void run() {
try {
api.configure("API_KEY",
"Bump User");
} catch (RemoteException e) {
Log.w("BumpTest", e);
}
}
}.start();
Log.d("Bump Test", "Service connected");
}
@Override
public void onServiceDisconnected(ComponentName className) {
Log.d("Bump Test", "Service disconnected");
}
};发布于 2012-07-05 17:07:22
在后台进程发出请求。
发布于 2012-07-05 17:07:38
主线程上的网络一个例外发生在2.2和3.0以及更高版本上,不同之处在于,在3.0和更高版本上,它们迫使您将涉及一些繁重或缓慢的操作的所有内容放在不同的线程中,正如您在asyncTask中所说的那样。
您只需创建一个内部asyncTask,并在其onBackground方法上放入您的api.configure :)
class LoadBumpAsyncTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
api.configure("9b17d663752843a1bfa4cc72d309339e", "Bump User");
} catch (RemoteException e) {
Log.w("BumpTest", e);
}
return null;
}
}只需在已连接的服务上调用new LoadBumpAsyncTask().execute(),即可正常工作。
https://stackoverflow.com/questions/11341032
复制相似问题