我是Android开发的新手,我正在查看之前在这里工作过的人的代码。我检查连接到服务器。这很好用(参见D / ConnectivityChecker(29802):isOnline = true)。然后我创建一个新的Intent并设置一个标志(public static int ONLINE_FLAG = 1),你可以在intent.setFlags 1中看到。然后我通过sendBroadcast将这个意图发送到我的connectionChangeReceiver。
如果我读出意图标志它不是1它是17!
这是connectionChangeReceiver:
的代码/**
* Receives notifications about connectivity changes
*/
private BroadcastReceiver connectionChangeReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("connectionChangeReceiver","Intent.Flags(): '" + intent.getFlags() + "' ICM.ONLINE_FLAG: '" + InternetConnectionManager.ONLINE_FLAG+"'");
setOnlineMode(intent.getFlags() == ICM.ONLINE_FLAG);
}
};
这是logcat:
05-08 14:28:05.615: V/ICM(29802): checkConnectivity
05-08 14:28:05.615: V/ICM(29802): ConnectivityChecker.execute()
05-08 14:28:05.620: D/ConnectivityChecker(29802): doInBackground
05-08 14:28:05.620: D/ConnectivityChecker(29802): Host: 192.168.2.109 Port:8000
05-08 14:28:05.670: V/ICM(29802): checkConnectivity
05-08 14:28:08.670: D/ConnectivityChecker(29802): isOnline=true
05-08 14:28:08.670: D/ConnectivityChecker(29802): intent.setFlags 1
05-08 14:28:08.670: D/ConnectivityChecker(29802): sendBroadcast(intent)
05-08 14:28:08.675: D/connectionChangeReceiver(29802): Intent.Flags(): '17' ICM.ONLINE_FLAG: '1'
intentFlag由ConnectivityChecker设置为1,但connectionChangeReceiver得到17.而且我不知道为什么?
我检查了两次。这是将创建Intent并调用函数setFlags()的代码的唯一部分。变量ONLINE_FLAG设置为“public static int ONLINE_FLAG = 1;”它可以用ICM.ONLINE_FLAG调用相同的变量。
我不知道为什么意图的标志是17 ??
也许有人可以帮我修复它。
编辑: 对不起,我忘记了创建意图的代码:
private class ConnectivityChecker extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
Log.d("ConnectivityChecker","doInBackground");
Socket socket = null;
boolean online = false;
try {
Log.d("ConnectivityChecker","Host: "+ConnectionHandler.host+" Port:"+ConnectionHandler.port);
socket = new Socket();
socket.bind(null);
InetSocketAddress address = new InetSocketAddress(ConnectionHandler.host, ConnectionHandler.port);
socket.connect(address, TIMEOUT * 1000);
online = socket.isConnected();
socket.close();
} catch (IOException e) {
// Nothing to do since we are just interested in the
// availability of the server
}
Log.d("ConnectivityChecker"," isOnline=" + online);
Log.d("ConnectivityChecker"," new Intent (" + CONNECTIVITY_ACTION+")");
Intent intent = new Intent(CONNECTIVITY_ACTION);
if (online){
intent.setFlags(ONLINE_FLAG);
Log.d("ConnectivityChecker","intent.setFlags " + ONLINE_FLAG);
}
isServerAvailable = online;
Log.d("ConnectivityChecker","sendBroadcast(intent)");
sendBroadcast(intent);
return null;
}
};
答案 0 :(得分:1)
setFlag()和getFlags()适用于specialized flags,不适用于自定义布尔数据。在您的情况下,您需要使用
intent.putExtra ( "tag", boolean_flag_sent );
并在您的接收地点
boolean_flag_received = intent.getBooleanExtra( "tag", false );
答案 1 :(得分:0)
抱歉,我不能正确理解你的问题。但根据我的理解,Intent.getFlags()将为您提供一个整数值,表示与intent本身关联的特殊标志的数量。在你的情况下它是17。 上面显示的代码不会创建任何新的意图,而是从其他代码传递创建的意图。检查调用此方法的位置以及传递的意图的代码