我找到了一个工作演示并成功使用它。但问题是我已经用最低版本 10 实现了它,其中 StrictPolicy 是可导入的。现在我希望 min 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