私は動作するデモを見つけ、それで正常に動作しました.しかし問題は、StrictPolicyがインポート可能な最小バージョン10で実装したことです.今、最小SDKバージョン1.6以上でBumpを動作させたい.
また、BindService の AsysncTask を使用できますが、うまくいかないことも確認しました。
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) {}
}
};
上記で使用した動作デモは docs.google.com/open?id=0B8f-N2PC8e0vU0gxRm1jaG51RGM です