2

每当我运行我的应用程序时,我都会收到“服务已连接”消息,但之后广播接收器没有响应。

问题是什么 ?谁能告诉我我该怎么办?

公共类 BumpTest 扩展 Activity { 私有 IBumpAPI api;

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("8836a26d3545410c9905d90528b2153a",
                          "ram");//8836a26d3545410c9905d90528b2153a,3826a0e77b284c05960d4513e87c4a88
        } 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");
    }
};

private final BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();
        try {
             Log.i("Bump Test", "In Receive " );
            if (action.equals(BumpAPIIntents.DATA_RECEIVED)) {
                Log.i("Bump Test", "Received data from: " + api.userIDForChannelID(intent.getLongExtra("channelID", 0))); 
                Log.i("Bump Test", "Data: " + new String(intent.getByteArrayExtra("data")));
            } else if (action.equals(BumpAPIIntents.MATCHED)) {
                long channelID = intent.getLongExtra("proposedChannelID", 0); 
                Log.i("Bump Test", "Matched with: " + api.userIDForChannelID(channelID));
                api.confirm(channelID, true);
                Log.i("Bump Test", "Confirm sent");
            } else if (action.equals(BumpAPIIntents.CHANNEL_CONFIRMED)) {
                long channelID = intent.getLongExtra("channelID", 0);
                Log.i("Bump Test", "Channel confirmed with " + api.userIDForChannelID(channelID));
                api.send(channelID, "Hello, world!".getBytes());
            } else if (action.equals(BumpAPIIntents.NOT_MATCHED)) {
                Log.i("Bump Test", "Not matched.");
            } else if (action.equals(BumpAPIIntents.CONNECTED)) {
                Log.i("Bump Test", "Connected to Bump...");
                api.enableBumping();
            }else{
                System.out.println("inn broadcast rcvr ::: "+api.toString());
            }
        } catch (RemoteException e) {}
    } 
};

@Override
public void onConfigurationChanged(Configuration newConfig) {
  super.onConfigurationChanged(newConfig);
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    bindService(new Intent(IBumpAPI.class.getName()),
                connection, Context.BIND_AUTO_CREATE);
    Log.i("BumpTest", "boot");

    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);

//txtReceived=(TextView)findViewById(R.id.txtReceived);
//bindService(new Intent(IBumpAPI.class.getName()), connection,  Context.BIND_AUTO_CREATE);
new BindService().execute();
}

@SuppressLint("NewApi")
class BindService extends AsyncTask<String, Integer, String>{

    @Override
    protected String doInBackground(String... params) {

        // TODO Auto-generated method stub
        bindService(new Intent(IBumpAPI.class.getName()), connection, Context.BIND_AUTO_CREATE);

        return null;
    }
    @SuppressLint("NewApi")
    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        System.out.println("done");
    }

}

 public void onStart() {
    Log.i("BumpTest", "onStart");
    super.onStart();
 }

 public void onRestart() {
    Log.i("BumpTest", "onRestart");
    super.onRestart();
 }

 public void onResume() {
    Log.i("BumpTest", "onResume");
    super.onResume();
 }

 public void onPause() {
    Log.i("BumpTest", "onPause");
    super.onPause();
 }

 public void onStop() {
    Log.i("BumpTest", "onStop");
    super.onStop();
 }

 public void onDestroy() {
    Log.i("BumpTest", "onDestroy");
    super.onDestroy();
    try {
        unbindService(connection);
        unregisterReceiver(receiver);
    } catch (Exception e) {
        // TODO: handle exception
    }
 }

}

4

0 回答 0