5

I have found a working demo and successfully worked with that.but the problem is i have implemented it with minimum version 10 where StrictPolicy is importable.now i want min sdk version 1.6 or above to work with Bump.

I have also seen that AsysncTask for BindService can be used but not worked for me.suggest something what wrong i am doing

 int sdkVersion = android.os.Build.VERSION.SDK_INT;
        if(sdkVersion>9)
        {
            try{
                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                StrictMode.setThreadPolicy(policy); 
            }catch (Exception e) {
                // TODO: handle exception
            }
        }   
    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);
    filter.addAction(BumpAPIIntents.DISCONNECTED);
    filter.addAction(BumpAPIIntents.BUMPED);
    this.registerReceiver(receiver, filter); 
    //txtReceived=(TextView)findViewById(R.id.txtReceived);
    //bindService(new Intent(IBumpAPI.class.getName()), connection,  Context.BIND_AUTO_CREATE);
    new BindService().execute();
}

        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;
        }
        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            System.out.println("done");
        }

    }

    /*@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }  */

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        try {
            unbindService(connection);
            unregisterReceiver(receiver);
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

    private final ServiceConnection connection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className, IBinder binder) 
        {
            Log.i("Bump", "onServiceConnected");

            api = IBumpAPI.Stub.asInterface(binder);

            try 
            {
                api.configure("KEY_HERE", "Some text..");
            }
            catch (RemoteException e) 
            {
                Log.i("Bump", "api.configured failed");
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName className) 
        {
            Log.i("Bump", "onServiceDisconnected");
        }
    };

    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            try {
                if (action.equals(BumpAPIIntents.DATA_RECEIVED)) {
                    Log.i("Bump", "DATA_RECEIVED");

                    Log.i("Bump", "Received data from: " + api.userIDForChannelID(intent.getLongExtra("channelID", 0)));
                    Log.i("Bump", "Data: " + new String(intent.getByteArrayExtra("data")));


                    //AlertDialog.Builder diaBuilder=
                } else if (action.equals(BumpAPIIntents.MATCHED)) {
                    Log.i("Bump", "MATCHED");
                    api.confirm(intent.getLongExtra("proposedChannelID", 0), true);

                } else if (action.equals(BumpAPIIntents.CHANNEL_CONFIRMED)) {
                    Log.i("Bump", "CHANNEL_CONFIRMED");
                    api.send(intent.getLongExtra("channelID", 0), (StaticData.strUserId + ","+ StaticData.strHomeName + ","+ StaticData.strUserImgUrl).getBytes());

                } else if (action.equals(BumpAPIIntents.CONNECTED)) {
                    Log.i("Bump", "CONNECTED");
                    api.enableBumping();
                } 
                else if (action.equals(BumpAPIIntents.DISCONNECTED)) {
                    Log.i("Bump", "DISCONNECTED");  

                } else if (action.equals(BumpAPIIntents.BUMPED)) {
                    Log.i("Bump", "BUMPED");  
                }

                else if (action.equals(BumpAPIIntents.NOT_MATCHED)) {
                    Log.i("Bump", "NOT_MATCHED");  
                }

                else if (action.equals(BumpAPIIntents.CHANNEL_CONFIRMED_EXTRA_CHANNEL_ID)) {
                    Log.i("Bump", "CHANNEL_CONFIRMED_EXTRA_CHANNEL_ID");  
                }

                else if (action.equals(BumpAPIIntents.CHANNEL_CONFIRMED_EXTRA_CHANNEL_ID)) {
                    Log.i("Bump", "CHANNEL_CONFIRMED_EXTRA_CHANNEL_ID");  
                }
            } catch (RemoteException e) {}
        }
    };

the working demo i have used above is docs.google.com/open?id=0B8f-N2PC8e0vU0gxRm1jaG51RGM

Saurabh Android
  • 657
  • 7
  • 14

0 Answers0