【问题标题】:Android Bump Api Network on main thread exception主线程异常上的 Android Bump Api Network
【发布时间】:2012-07-05 15:54:31
【问题描述】:

首先,我对 Android 和 JAVA 世界很陌生(来自 C/C++/Objective-C)。 我正在尝试集成 Android Bump API(3.0,最新版本),但遇到了麻烦。 我复制了这个例子,它在Android 2.2下运行良好,bump服务正常启动,但对于Android 3.0及更高版本它不起作用。 我在加载我的活动时遇到了一个异常(主线程上的网络),我知道这个异常以及如何避免它,但是在这种情况下,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 中运行它,但是如何以及为什么?

【问题讨论】:

    标签: java android multithreading exception android-asynctask


    【解决方案1】:

    我在这个问题上停留了一天左右......从字面上看,在将它发布到这里 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");
        }
    };
    

    【讨论】:

      【解决方案2】:

      主线程上的网络在 2.2 和 3.0 及更高版本中发生了一个异常,不同之处在于,在 3.0 及更高版本上,它们会强制您将涉及一些繁重或慢速操作的所有内容放在不同的线程中,正如您在一个异步任务。

      您只需创建一个内部 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() 即可。

      【讨论】:

        【解决方案3】:

        在后台进程中发出请求。

        【讨论】:

        • 事实上我已经找到了答案,我在这个问题上坚持了一天......并且在发布它后 2 分钟我解决了它...... :)
        猜你喜欢
        • 2013-06-26
        • 2016-07-29
        • 1970-01-01
        • 1970-01-01
        • 2020-07-18
        • 2020-07-16
        • 2016-07-22
        • 1970-01-01
        • 2013-02-17
        相关资源
        最近更新 更多